C Assignment 1 (Array)
C Assignment 1 (Array)
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;
}
#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;
}
#include <stdio.h>
int main()
{
int HILLARY[50],TRUMP[50],i,n,h=0,t=0;
return 0;
}
#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;
}
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;
}
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;
}
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.