(Self-Made) Test 2
(Self-Made) Test 2
b) char name[64];
FILE *f = fopen("/tmp/name", 'r');
fscanf(*f, "%s", name);
fclose(f);
Question 2
What are the outputs of the programs given below?
a) #include <stdio.h>
int main()
{
int a[3][4] = {
{30, 79, 59, 74},
{88, 53, 40, 80},
{61, 89, 58, 71},
};
for (int i=0; i<3; i++)
{
printf("%d ", a[i][2]);
}
return 0;
}
b) #include <stdio.h>
int main()
{
int a[3][4] = {
{30, 79, 59, 74},
{88, 53, 40, 80},
{61, 89, 58, 71},
};
for (int i=0; i<3; i++)
{
printf("%d ", a[2][i]);
}
return 0;
}
c) #include <stdio.h>
int main()
{
int a[3][4] = {
{60, 92, 42, 25},
{90, 82, 27, 20},
{38, 71, 97, 26},
};
for (int i=0; i<3; i++)
{
for (int j=0; j<4; j++)
{
printf("%d ", a[i][j]);
}
printf("\n");
}
return 0;
}
d) #include <stdio.h>
int main()
{
int s=0;
int a[3][4] = {
{75, 41, 41, 73},
{45, 52, 97, 45},
{95, 86, 96, 61},
{20, 85, 47, 75},
};
for (int i=0; i<3; i++)
{
s += a[i];
}
printf("%d ", s);
return 0;
}
Question 3
Try to write missing statement
a) #include <stdio.h>
int main()
{
char name[200], phone[12];
FILE *f = fopen("data.txt", "w");
printf("Name: ");
...................................
printf("Phone number: ");
...................................
printf("Saving string '%s - %s' to file...\n", name, phone);
...................................
fclose(f);
return 0;
}
b) #include <stdio.h>
int main()
{
int n, sum=0;
printf("n: ");
scanf("%d", &n);
.....................................
.....................................
.....................................
.....................................
printf("Sum from 1 to n is: %d\n", sum)
return 0;
}
c) #include <stdio.h>
int get_min_value(int arr[]);
int main()
{
int a[] = {12,75,23,38,90,11,22,18,01};
printf("Min value is %d\n",
get_min_value(a));
return 0;
}
int get_min_value(int arr[])
{
................................
................................
................................
................................
................................
................................
................................
................................
................................
}
d) #include <stdio.h>
int is_leap_year(int year)
{
..............................
..............................
..............................
..............................
..............................
..............................
..............................
..............................
..............................
}
int main()
{
int year;
printf("Year: ");
..............................
if (is_leap_year(year))
printf("%d is a leap year", year);
else
printf("%d is NOT a leap year", year);
return 0;
}