Ad

Chapter - 9: Arrays

What would be the output of the following programs:


Ad
E
Sections
1
Exercises

(a)

#include<stdio.h>
int main()
{
	int b[] = { 10, 20, 30, 40, 50 };
	int i;
	for (i = 0; i <= 4; i++)
		printf("\n%d" *(b + i));
	return 0;
}

Error: There will be a comma before the variable in printf function.


(b)

#include<stdio.h>
int main()
{
	int b[] = { 0, 20, 0, 40, 5 };
	int i, *k;
	k = b;
	for (i = 0; i <= 4; i++)
	{
		printf("\n%d" *k);
			k++;
	}
	return 0;
}

Error: There will be a comma before the variable in printf function.


(c)

#include<stdio.h>
void change(int*, int);
int main()
{
	int a[] = { 2, 4, 6, 8, 10 };
	int i;
	change(a, 5);
	for (i = 0; i <= 4; i++)
		printf("\n%d", a[i]);
	return 0;
}

void change(int *b, int n)
{
	int i;
	for (i = 0; i < n; i++)
		*(b + i) = *(b + i) + 5;
}

Output:

7
9
11
13
15


(d)

#include<stdio.h>
int main()
{
	static int a[5];
	int i;
	for (i = 0; i <= 4; i++)
		printf("\n%d", a[i]);
	return 0;
}

Output:

0
0
0
0
0


(e)

#include<stdio.h>
int main()
{
	int a[5] = { 5, 1, 15, 20, 25 };
	int i, j, k = 1, m;
	i = ++a[1];
	j = a[1]++;
	m = a[i++];
	printf("\n%d %d %d", i, j, m);
	return 0;
}

Output: 3 2 15

Ad

© 2024 Garbage Valuegarbage value logo