[go: up one dir, main page]

0% found this document useful (0 votes)
72 views6 pages

Assignment 4

This document contains the solutions to 15 programming assignments submitted by a student. The assignments cover topics like arrays, passing arrays to functions, static arrays, and finding the binary equivalent of a decimal number. For each question, the student provides the code to solve the problem along with an explanation of the output or any errors. The last two questions ask about what happens if an array is initialized with more values than its size, and what gets passed when an array is passed as a function argument.

Uploaded by

Gourav Gupta
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)
72 views6 pages

Assignment 4

This document contains the solutions to 15 programming assignments submitted by a student. The assignments cover topics like arrays, passing arrays to functions, static arrays, and finding the binary equivalent of a decimal number. For each question, the student provides the code to solve the problem along with an explanation of the output or any errors. The last two questions ask about what happens if an array is initialized with more values than its size, and what gets passed when an array is passed as a function argument.

Uploaded by

Gourav Gupta
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
You are on page 1/ 6

SUBMITTED BY:-

Aditya Vashishtha
B.TECH(HONS)-CSE-MBA(INTGR)
Regd.no. 10804952
Roll no:-B 39
Section no:-256

Assignment 4
1. #include<stdio.h>
void main()
{
int num[26],temp;
num[0]=100;
num[25]=200;
temp=num[25];
num[25]=num[0];
num[0]=temp;
printf("\n %d %d", num[0],num[25]);k
}
Answer-
output.
200 100

2. #include<stdio.h> /* Find out the errors, if any: */
void main()
{
int size;
scanf("%d",&size);
int arr[size];
for(i=1;i<=size;i++)
{
scanf("%d",arr[i]);
printf("%d",arr[i]);
}
}
Answer-We cannot take variables inside the square brackets at the time of declaration of an array...


// Find error, if any.

3. #include<stdio.h>
void main()
{
int i,a=2,b=3;
int arr[2+3];
for(i=0;i<a+b;i++)
{
scanf("%d",&arr[i]);
printf("\n%d",arr[i]);
}
}
AnsError is in the for loop as we have not used brackets at the place {i<a+b} ,in this statement brackets
should be used before a and after b..




4. What is the difference between the 5's in these two expressions?
int num[5];
num[5]=11;
Ansin the first statement 5 is used as the size of array num....while in the second statement 5 is used to
initialize value at 5
th
position in array num...

5. Twenty-five numbers are entered through keyboard into an array. Write a program to find out how
many of them are positive, negative, even and odd.
ANS-

#include<stdio.h>
#include<conio.h>
void main()
{
int b[25],pos=0,neg=0,even=0,odd=0;
clrscr();
printf(\n input the 25 values );

for(int i=0;i<25;i++)
scanf(%d ,&b[i]);

for(i=0;i<25;i++)
{
If(b[i]>0)
pos++;
if(b[i]<0)
neg++;
If(b[i]%2==0)
even++;
else
odd++;
}
printf(\n no of positive nos are=%d.pos);
printf(\n no of negative nos are=%d.neg);
printf(\n no of even nos are=%d.even);
printf(\n no of odd nos are=%d,odd);

getch();
}


6. Write a program to interchange the odd and even elements of the array.
ANS:-

#include<stdio.h>
#include<conio.h>
void main()
{

int b[],n,temp,i;
clrscr();
printf(\n input the SIZE OF THE ARRAY \n);
scanf(%d,&n);
for(int i=0;i<n;i++)
scanf(%d ,&b[i]);
for(i=0;i<n;i+=2)
{
temp=b[i];
b[i]=b[i+1];
b[i+1]=temp;
}


printf(\n NEW ARRAY IS--\n);
for(i=0;i<n;i+=2)
printf( %d,b[i]);
getch();
}



7.
#include<stdio.h>
void main() // Write output
{
int b[]={0,20,0,40,5};
int i, *k;
k=b;
for(i=0;i<=4;i++)
{
printf("\n%d",*k);
k++
}
}
ANSWER:--
0
20
0
40
5

8. #include<stdio.h>
void change(int *,int); //Write output
void main()
{
int a[]={2,4,6,8,10};
int i;
change(a,5);
for(i=0;i<=4;i++)
printf("\n%d",a[i]);
}
void change(int *b, int n)
{
int i;
for(i=0; i<n; i++)
*(b+i)=*(b+i)+5;
}
ANSWER:-
7
9
11
13
15





9. #include<stdio.h>
void main()
{
static int a[5];
int i;
for(i=0;i<=4;i++)
printf("\n%d",a[i]);
} // Write output.
Answer:-
0
0
0
0
0

10. Write a program to copy the contents of one array into another in reverse order.
ANSWER:-
#include<stdio.h>
#include<conio.h>
Void main()
{
int a[4],b[4],i,j;
clrscr();

for(i=0;i<4;i++)
scanf(%d,a[i]);
for(i=4;i>0;i--)
b[i]=a[j++];

for(i=0;i<4;i++)
printf(\n new array is-\n%d,b[i]);
getch();
}



11. #include<stdio.h> // Write output
void junk(int *,int *);
void main()
{
int i=5,j=2;
junk(&i,&j);
printf("\n %d %d",I,j);
}
void junk(int *i,int *j)
{
*i=*i * *i;
*j=*j * *j;
}
Output will be:-
25 4





12. #include<stdio.h> // Write output
void junk(int *,int);
void main()
{
int i=4,j=2;
junk(&i,j);
printf("\n%d %d",I,j);
}
void junk(int *i, int j)
{
*i=*i * *I;
j=j*j;
}
Answer:-
Output will be:-
4,2


13. #include<stdio.h>
void main() // Write output
{
float a=13.5,*b,*c;
b=&a;
c=b;
printf("\n%u %u %u", &a, b, c);
printf("\n%f %f %f %f %f", a,*(&a),*&a,*b,*c);
}
Answer:-
13.5 13.5 13.5
13.5 13.5 13.5 13.5 13.5


14. Write a function to find the binary equivalent of a given decimal integer and display it.

Answer:-
##include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
Int rem,num,new=0;
clrscr();
printf(\n input the binary equivalent);
scanf(%d, &num);
while(num>0);
{
rem=num%10;
new+=pow(2,0);
num=num/10;
}
printf(\n new num is=%d,new);
getch();
}
15. What will happen if you try to put so many values into an array when you initialize it, such that the
size of the array is exceeded? When you pass an array as an argument to a function, what actually gets
passed?
Answer:-
In this case, at the time of execution of the program this program, there will be an error...as the size of the
array has been exceeded than the declaration...
In the case of passing an array as the argument to the function, only the array name gets passed ...

You might also like