You are on page 1of 13

GLA UNIVERSITY

C PROGRAMMING
ASSIGNMENT

3/23/2020

SUBMITTED BY-
SAKSHAT LAKHIANI
SEC- T
ROLL NO.- 37.
One-Dimensional Array

Experiment List
 Write a program in C to copy the elements one array into
another array.
ANS-
#include<stdio.h>
int main()
{
int array1[50],array2[50],i,n;
printf("\nEnter the size of array.");
scanf("%d",&n);
printf("\nEnter the elements now- \n");
for(i=0;i<n;i++)
{
scanf("%d",&array1[i]);
}
for(i=0;i<n;i++)
{
array2[i]=array1[i];
}
printf("\nOriginal Array1 =");
for(i=0;i<n;i++)
{
printf(" %d",array1[i]);
}
printf("\nCopied Array2 = ");
for(i=0;i<n;i++)
{
printf(" %d",array2[i]);
}
return 0;
}
 Write a program which takes 10 inputs in an integer array
and display their values in the reverse order.
ANS-
#include <stdio.h>
int main()
{
int ary[10],i;
printf("\nEnter the 10 elements of array :\n");
for(i=0;i<=9;i++)
{
scanf("%d\n",&ary[i]);
}
printf("\nReversed Array elements :\n");
for(i=9;i>=0;i--) //loop for copying elements
{
printf("%d\n",ary[i]);
}
return 0;
}

 Write a program to count the number of positive and


negative numbers in an array as input by the user.
ANS-

#include <stdio.h>
int main()
{
int A[100],i,n,positive=0,negative=0;
printf("Enter the size of the array you want:");
scanf("%d",&n);
printf("\nEnter the elements of array :\n");
for(i=0;i<n;i++)
{
scanf("%d",&A[i]);
}
for(i=0;i<n;i++)
{
if(A[i]>0)
positive++;
else
negative++;
}
printf("\nThe no. of Positive integers in array=%d",positive);
printf("\nThe no. of Negative integers in array=%d",negative);

return 0;
}

 In USA President election, there are 50 states, you have to


create two arrays HILLARY and TRUMP store the number
of vote polled in each state. Write a program to show
result in each state and also find who is the winner?
ANS-

#include <stdio.h>
int main()
{
int HILLARY[50],TRUMP[50],i,n,h=0,t=0;

printf("\nEnter the votes of Trump:\n");


for(i=0;i<50;i++)
{
scanf("%d",&TRUMP[i]);
}
printf("\nEnter the votes of Hillary:\n");
for(i=0;i<50;i++)
{
scanf("%d",&HILLARY[i]);
}
for(i=0;i<50;i++)
{
if(TRUMP[i]>HILLARY[i])
{
printf("\nWinner of state (%d)= TRUMP",i+1);
t++;
}
else
{
printf("\nWinner of state (%d)= HILLARY",i+1);
h++;
}
}
if(t>h)
{
printf("\n\n THE WINNER AND NEW PRESIDENT OF USA IS-
DONALD TRUMP.");
}
else
{
printf("\n\n THE WINNER AND NEW PRESIDENT OF USA IS-
HILLARY.");
}

return 0;
}

 Write a program in C to separate odd and even integers in


separate arrays.
ANS-

#include <stdio.h>
int main()
{
int A[50],odd[50],even[50],i,n,j=0,k=0,a,b;
printf("\nEnter the number of elements you want to store:(max-50)");
scanf("%d",&n);
printf("\nEnter the values in array:\n");
for(i=0;i<n;i++)
{
scanf("%d",&A[i]);
}

for(i=0;i<n;i++)
{
if((A[i]%2)==0)
{
even[j]=A[i];
j++;
}
else
{
odd[k]=A[i];
k++;
}
}
printf("\nEVEN ARRAY= \n");
for(a=0;a<j;a++)
printf("%d ",even[a]);
printf("\nODD ARRAY= \n");
for(b=0;b<k;b++)
printf("%d ",odd[b]);

return 0;
}
 There is a class of 10 students having roll numbers from 1
to 10. One of the students in class is missing. How will you
find the roll no. of that missing student?
ANS-

#include <stdio.h>
#include <math.h>
int main()
{
int a[10],i,j,s=0;
printf("\nEnter the roll no. of students who are present- \n");
for(i=0;i<9;i++)
{
scanf("%d",&a[i]);
}
for(i=0;i<9;i++)
{
s=s+a[i];
}
j=(1+2+3+4+5+6+7+8+9+10)-s;
printf("\nABSENT STUDENT roll number= %d",j);

return 0;
}

 Find the binary equivalent of an integer number using


array.
#include <stdio.h>
#include <math.h>
int main()
{
int binary[50],i,j,k,a,b,c,n;
printf("Enter the INTEGER number: ");
scanf("%d",&n);
a=sqrt(n);

for(i=a;i>=0;i--)
{
b=n%2;
binary[i]=b;
n=n/2;
}
printf("\n The binary equivalent of the integer is= ");
for(i=0;i<=a;i++)
{
printf("%d ",binary[i]);
}

return 0;
}
Tutorial Sheet
Q1. Consider a scenario where there are two classes A and B having
30 students each. A test was conducted for both the classes in a
single room and student having same class roll number but from
different classes was made to sit together and as a result they copied
from each other and scored equal marks. Wap in ‘C’ to compute the
marks of class B students based on the marks of class A students
(using Arrays).
#include <stdio.h>
#include <math.h>
int main()
{
int A[30],B[30],i,j,k;
printf("FOR CLASS- A\n");
for(i=0;i<30;i++)
{
printf("\nEnter the marks of Roll No. %d: ",i+1);
scanf("%d",&A[i]);
}
for(i=0;i<30;i++)
B[i]=A[i];
printf("\nFOR CLASS- B\n");
for(i=0;i<30;i++)
{
printf("\nMarks of roll no. %d: %d",(i+1),B[i]);
}
return 0;
}

Q2. Write a program in ‘C’ to store (in an array) and print the roll
numbers of students beginning from m to n.
#include <stdio.h>
#include <math.h>
int main()
{
int A[1000],m,n,i,j,k,d;
printf("Enter the range (m to n) =\n");
scanf("%d%d",&m,&n);
d=(n-m)+1;
for(i=0;i<d;i++)
{
A[i]=m;
m++;
}
for(i=0;i<d;i++)
{
printf("\n%d",A[i]);
}
return 0;
}

Q3. Write a program in ‘C’ to calculate standard deviation.


#include <stdio.h>
#include <math.h>
int main()
{
float n,avg, v=0.0, standardDV;
int i,sum=0,a[100];
printf("\nEnter the no. of elements you want to store in array.: ");
scanf("%f",&n);
printf("\n Enter the elements:\n");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}

for(i=0;i<n;i++)
sum=sum+a[i];
avg=(float)sum/n;
for(i=0;i<n;i++)
{
v=v+pow(a[i]-avg,2);
}
v=v/n;
standardDV=sqrt(v);
printf("\nThe standard deviation of these data values is: %f",standardDV);
return 0;
}

Q4. A program P reads in 500 integers in the range [0..100]


representing the scores of 500 students. It then prints the frequency
of each score above 50. What would be the best way for P to store
the frequencies? Write the above program in ‘C’.
#include <stdio.h>
#include <math.h>
int main()
{
int a[500],b[50],i,j,k;
for(i=0;i<50;i++)
b[i]=0;
printf("Enter the marks of 500 students one by one: ");
for(i=0;i<500;i++)
{
printf("\nEnter the marks of student %d: ",i+1);
scanf("%d",&a[i]);
if(a[i]>50)
{
b[(a[i]-51)]++;
}
}
printf("\nFREQUENCIES of greater than 50 marks-\n")
for(i=0;i<50;i++)
{
printf("\nFrequency of %d= %d",(i+51),b[i]);
}
return 0;
Find the output (Q6-Q8)
Q6
#include<stdio.h>
int main(){
int arr[5];
arr[0] = 5;
arr[2] = -10;
arr[3/2] = 2;
arr[3] = arr[0];
printf("%d %d %d %d", arr[0], arr[1], arr[2], arr[3]);
return 0;
}
OUTPUT-
5 2 -10 5

EXPLANATION-
Array arr is declared of size 5. So it can hold 5 integer values.
Values are initialized in the array.
Position 0 holds 5
Position 2 holds -10
Position (3/2=1) holds 2
Position 3 holds the same value as position 0 i.e- 5
Now printf command is used to print values at position 0,1,2,3.
So the output values will be: 5 2 -10 5 respectively..

Q7.
#include<stdio.h>
int main(){
int arr[2] = {10, 20, 30, 40, 50};
printf("%d %d %d %d %d",arr[0],arr[1],arr[2],arr[3],arr[-
1]);
return 0;
}
OUTPUT-
10 20 0 0 23424

EXPLANATION-
Array arr is declared of size 2 and values are initialized in the array
Since the size of array declared is 2, we can control only 2 values and
therefore during initializing it will hold only the first two values.
i.e -10,20. At position 0 and 1 respectively.
Now printf command is used to print values at position 0,1,2,3,-1.
So the output values will be: 10 20 for the position 0 and 1. All the rest
positions will print garbage values.
Note: compiler will give warning for initializing over the size limit.

Q8.
#include<stdio.h>
int main(){
int arr[10] = {1,2,3};
printf("%d %d %d %d",arr[0],arr[2],arr[4],arr[6]);
return 0;
}
OUTPUT-
1300

EXPLANATION-
Array arr is declared of size 10 and values are initialized in the array
Since the size of array declared is 10,it is capable of holding 10 values,
but we are initializing it with only three values- 1,2,3,; which will be held
in the positions 0,1,2 respectively. Rest other positions are not initialized
therefore they will hold GARBAGE VALUES.
Now printf command is used to print values at position 0,2,4,6.
So the output values will be: 1 3 for the position 0 and 2. All the rest
positions will print garbage values.

You might also like