0% found this document useful (0 votes)
18 views4 pages

(Self-Made) Test 2

my brother also made this for me

Uploaded by

tonynghia0812
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)
18 views4 pages

(Self-Made) Test 2

my brother also made this for me

Uploaded by

tonynghia0812
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/ 4

Question 1

Identity and correct the errors in each of the following:


a) switch(n%2){
case 0
print("%d is even number", n)
break
case 1:
print("%d is odd number", n)
break
}

b) char name[64];
FILE *f = fopen("/tmp/name", 'r');
fscanf(*f, "%s", name);
fclose(f);

c) FILE f = fopen("/tmp/hihi", 'w');


fprintf(f, "HelloWorld");
close(f);

d) float a[] = {1.1; 3.14};


printf("%lf", a[1] + a[2])

e) float a[2][3] = {{1.1, 2.2, 3.3}, {4.4, 5.5, 6.6}};


printf("%f", a[0][0] + a[1][1]);
printf("%d", a[0][1] + a[2][3]);

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;
}

You might also like