0% found this document useful (0 votes)
5 views7 pages

C Programming: Array Operations Guide

Uploaded by

yafafo5767
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views7 pages

C Programming: Array Operations Guide

Uploaded by

yafafo5767
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Date:13/1/2020

Roll No. and name:20tec014- Harinee Rathod


Course code and name:2CS101 Computer programming

Practical no:5(a)

Aim:
Write a program
i. To read data from keyboard and store into 1-D array
ii. To read data from array and copy its square back to another array
iii. To reverse all elements of original array
iv. To find out maximum element of an original array and print its location

Methodology followed:
#include <stdio.h>
#include <stdlib.h>

int main()
{
int n,i,k,loc,temp,max;
printf("enter the number:");
scanf("%d",&n);
int arr[n],arr2[n];
//(1)
for (i=0;i<n;i++)
{
printf("enter the value of an array ");
scanf("%d",&arr[i]);
}
for (i=0;i<n;i++)
{
printf("arr[%d]:%d\n",i,arr[i]);
}
//(2)
for (i=0;i<n;i++)
{
arr2[i]=arr[i]*arr[i];
}
for (i=0;i<n;i++)
{
printf("the value of square of arr is arr2[%d]:%d\n",i,arr2[i]);
}
//(3)
k=n-1;
i=0;
while (i<k)
{
temp=arr[i];
arr[i]=arr[k];
arr[k]=temp;
i++;
k--;
}
printf("\n\n");
for (i=0;i<n;i++)
{
printf("the reverse of the array is:\n");
printf("arr[%d]:%d\n",i,arr[i]);
}
//(4)
max=arr[0];
for (i=0;i<n;i++)
{
if(max<arr[i])
arr[0]=arr[i];
loc=i+1;
}
printf("the maximum array is %d\n and the location of the array is %d\n",max,loc);

return 0;
}

Theoretical principle used:


● One dimensional array
● Nested for loop
input/output:
Practical no:5(b)

Aim:Write a program to delete an element from 1-D array.

Methodology followed:
#include <stdio.h>
#include <stdlib.h>

int main()
{
int n,i,position;
printf("enter the number of array:");
scanf("%d",&n);
int arr[n];
for (i=0;i<n;i++)
{
printf("enter the value of array:");
scanf("%d",&arr[i]);
}
for(i=0;i<n;i++)
{
printf("arr[%d]:%d\n",i,arr[i]);
}
printf("enter the position number which you want to delete");
scanf("%d",&position);
printf("the position of the array is %d\n",position);
for(i=position-1;i<=n-1;i++)
{
arr[i]=arr[i+1];
}
for (i=0;i<n-1;i++)
{
printf("arr[%d]:%d\n",i,arr[i]);

Theoretical principle used:


● One dimensional array
● Nested loops
● if statement

Input/output:
Practical no:5(c)
Aim:
Write a program that fills a 5 x 5 matrix with the following data:
i. Upper left triangle with -1
ii. Lower right triangle with 1
iii. Right to left diagonal with 0
Display the matrix on the screen.
Methodology followed:

#include <stdio.h>
#include <stdlib.h>

int main()
{
int n,m;
printf("enter the number of row and column\n");
scanf("%d",&n);
scanf("%d",&m);
int array[n][m],i,j;
if (n==m)
{
for(i=0;i<n;i++)
{
for (j=0;j<m;j++)
{
if (i+j<=n-2)
array[i][j]=-1;
else if (i+j==n-1)
array[i][j]=0;
else
array[i][j]=1;
}
}
}
for (i=0;i<n;i++)
{
for (j=0;j<m;j++)
{
printf("%d",array[i][j]);
printf("\t");
}
printf("\n");
}
return 0;
}

Theoretical principle used:


● 2D array
● Nested for loop

Input/output :

Practical no:5(d)
Aim:
Suppose that a class has 5 students. Each student study four subjects; CP, CS,
Maths,
and Physics. Make a 2D array for the same. Write a C program
i. To find total marks in all subjects obtained by each student.
ii. To find average marks obtained by all 5 students in C programming subject.

Methodology followed:
#include <stdio.h>
#include <stdlib.h>
/*Suppose that a class has 5 students. Each student study four subjects; CP, CS,
Maths,
and Physics. Make a 2D array for the same. Write a C program
i. To find total marks in all subjects obtained by each student.
ii. To find average marks obtained by all 5 students in C programming subject*/

int main()
{
int n,m,i,j,sum=0;
float avg;
printf("the code of subject is maths:1 physics:2 computer programming:3 cs:4\n");
printf("enter the number of students and number of subjects\n");
scanf("%d",&n);
scanf("%d",&m);
int arr[n][m];
for (i=0;i<n;i++)
{
printf("enter the marks of student %d\n",i+1);
for (j=0;j<m;j++)
{
printf("in subject %d :",j+1);
printf("arr[%d][%d]:",i,j);
scanf("%d",&arr[i][j]);
}
}

for (i=0;i<n;i++)
{
;
for (j=0;j<m;j++)
{
sum=sum+arr[i][j];
}
printf("total marks of student %d is=%d\n",i+1,sum);
printf("avg marks of student is=%f\n",(float)sum/m);
sum=0;
}

return 0;
}
Theoretical principle used:
● Multidimensional array
● Nested for loop

Input/output:

Conclusion:
From the above practice we learnt to nested for loop , one dimensional array and
multidimensional arrays.

Common questions

Powered by AI

Using nested loops for filling a matrix results in a time complexity of O(n^2) where n is the dimension size. Each cell requires constant time for assignment, but large matrices can significantly increase execution time and memory usage due to iterating through all matrix cells. Optimal performance in practical applications requires careful consideration of matrix size .

To find the maximum element and its position in a 1-D array, iterate through the array with a for loop, comparing each element to a variable `max` initialized to the first element. When a larger element is found, update `max` and store the current index as `loc`. Continue until the end of the array to determine the maximum element and its last occurrence location .

The conditional `if` structures in matrix manipulation assess the indices' positions to assign values. It determines which triangle or diagonal a particular cell resides in, ensuring each is assigned the correct value (-1, 0, or 1). This logic effectively segments the matrix into regions, facilitating targeted value assignment .

The program uses a nested loop to traverse the 2D array: for total marks, a variable `sum` accumulates marks for each student across subjects, which resets per student. For the average in a specific subject, a separate accumulation loop calculates the sum over all students for the subject column and then divides by the number of students .

The methodology uses a `while` loop to reverse a 1-D array. Two pointers, `i` and `k`, point to the start and end of the array, respectively. The elements at these positions are swapped, then the pointers move towards the center. This process repeats until the pointers meet or cross .

Dynamic sizing of arrays, using `scanf` to determine user input for dimensions, enhances program flexibility by allowing custom-sized data structures matching input requirements. However, it requires careful memory management to prevent issues such as buffer overflow, emphasizing the need for efficient resource allocation and error checking in larger applications .

Variables such as `sum` are crucial for accumulating data – here, they sum grades per student or subject. Within the loops, `sum` initializes to 0 for each new student or operation to avoid carryover errors. Loop counters like `i` manage array indexes, ensuring each student's or subject's data is correctly accessed and processed .

Errors might include attempting to delete an element at a non-existent index or failing to update the array size, leading to incorrect data access. Mitigation involves validating the index range before deletion and updating the size appropriately, often by allocating a new array or tracking valid bounds .

To delete an element at a specified position, shift all elements following the specified index one position to the left. This is done using a for loop starting at `position-1`, copying the next element into the current position. Finally, the array size is reduced by one as the last element is now effectively removed .

A 5x5 matrix can be structured using nested loops. The loops iterate over each cell: if the sum of indices `i + j` is less than `n-1`, assign -1 for the upper left triangle; if it equals `n-1`, assign 0 for the diagonal; otherwise, assign 1 for the lower right triangle. This decision structure manages the cell assignments based on their relative positions .

You might also like