Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Q1. Find the error in each of the following program segments and correct the errors.
a. int x[5]={1,2,3,4,5};
for(int i=0;i<=5;i++)
printf(%d,x[i]);
b. float
a[3][4]={1.1,2.0,3.3,4.5,5.3,6.7,7.6,8.0,9.1,10.2,11.4,12
.4,13.6,14.9};
c. void h(int m,int n)
{int q;
q=m%n;
return q;
}
d. while(d>0)
printf(%d,d--);
e. for(int k=10;k>5;k++)
printf(%d,k*2);
Q2. Determine the output of the following 5 C program segments below. You must show all
the output.
a. int x[4][3]={{1,2},5,6,8,7,9,10,11,14};
int y[4][3]={1,2,5,6,8,7,9,10,11,14};
for(int a=0;a<=3;a++)
for(int b=0;b<=2;b++)
{printf(%d,x[a][b]);
printf(%d,y[a][b]);}
e. void f();
int main()
{
f();
f();
return 0;
}
void f(int l)
{static int l=1;
++l;
printf((l is %d,l);
l++;
printf(l is %d,l);
printf(l is %d,l++);
printf(l is %d,l++);
printf(l is %d,l);
}
Q3:
Given the following, answer the questions below:
#define MAX 50
int a[MAX], i,temp;
i. What is the effect of this program segment?
for
(i = 0; i < MAX / 2;
temp = a[i];
a[i] = a[MAX - i - 1];
a[MAX - i - 1] = temp;
++i) {
}
(a)
(b)
(c)
(d)
(e)
(a)
(b)
(c)
(d)
(e)
iii.
(i = 0; i < MAX - 1;
if (a[i] > a[i + 1]) {
temp = a[i];
a[i] = a[i + 1];
a[i + 1] = temp;
}
(a)
(b)
(c)
(d)
(e)
++i)
++i)
iv.
(a)
(b)
(c)
(d)
(e)
0
49
50
a[50]
none of the above
0
1
any negative number
There is no minimum.
none of the above
Q4. Trace the following program and write down the exact output of this program in the box
shown.
#include <stdio.h>
#define ROWS 4
#define COLS 4
int multi[ROWS][COLS];
int main(void)
{
int row, col;
for (row = 0; row < ROWS; row++)
{
for (col = 0; col < COLS; col++)
{
multi[row][col] = row*col;
}
}
for (row = 0; row < ROWS; row++)
{
printf("\n");
for (col = 0; col < COLS; col++)
printf("%d ",multi[row][col]);
printf("\n");
}
return 0;
}