1 HCMC University of Technology and Education Faculty for High Quality Training ECET Solution Final exam at the end of the semester 2, 2016-2017 Subject: Introduction to C programming
Trang 11
HCMC University of Technology and Education
Faculty for High Quality Training
ECET
Solution (Final exam at the end of the semester 2, 2016-2017) Subject: Introduction to C programming language Subject code: CPRL130064E
Number of pages: 06 pages Duration: 75 minutes
Used paper documents
Students work directly on the exam and submit all
Supervisor Signature 1 Supervisor Signature 2
Points and Signatures
The first marking
lecture
The second marking lecture First and last name:
Student ID: Order number: Room:
Question 1: (2.0 points) Give a following program, please perform:
a) Draw a flowchart? (1 point)
b) Show results on the screen after performing the program? (1 point)
# #include <stdio.h>
I t main(void)
{
int a = 12, b = 6, c = 17, d;
if ((a >= b) || (b<c))
{
a +=++ c;
b = a - c;
d = c%b;
}
else
{
b += c;
c = ++a - b;
d = (b + c) / a;
}
printf("a:%d\n", a);
printf("b:%d\n", b);
printf("c:%d\n", c);
printf("d:%d\n", d);
return 0;
}
a) a flowchart
a = 12, b = 6, c = 17;
(a >= b) || (b<c)
a +=++ c;
b = a - c;
d = c%b;
b += c;
c = ++a - b;
d = (b + c) / a;
Print: a, b, c,d
T
F Begin
End
b) Results on the screen a:29 (0.25 point) b:11 (0.25 point) c:18 (0.25 point) d:7 (0.25 point)
Trang 22
Question 2: (2.0 points) Give a following program, please perform:
a) Show results on the screen after performing the program? (1 point)
b) Write Pseudocode? (0.5 point)
c) Rewrite this program using “do…while”? (0.5 point)
#include <stdio.h>
void main()
{
int i;
for (i = 15; i>0; i)
printf("%d \t", i);
}
a) Results on the screen:
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1
b) Pseudocode:
Set i to 15
While i is larger than 0
Print i on the screen
Decrease i by 1
c) Rewrite using “do…while”:
#include <stdio.h>
void main() {
int i;
i = 15;
do printf("%d \t", i);
while ( i > 0);
}
Question 3: (1.0 points) What does the following program print?
#include<stdio.h>
int main (void)
{
unsigned int row = 4; // initialize row
unsigned int column; // define column
while (row >= 1) // loop until row < 1
{
column = 1; // set column to 1 as iteration begins while (column <= 10) // loop 10 times
{
if (row % 2 == 0)
printf("%s", "@"); // output else
break;
++column; // increment column } // end inner while
row; // decrement row
Trang 33
printf("\n"); // begin new output line } // end outer while
} // end function main
@@@@@@@@@@ (0.25 point)
(0.25 point)
@@@@@@@@@@ (0.25 point)
(0.25 point)
Question 4: (1.0 points) Show results on the screen after performing a following program:
#include <stdio.h>
void main()
{
int a[6] = { 1, 2, 3, 4, 5, 6};
int *p1 = &a[1];
int *p2 = &a[3];
*p1 = *p2 + 4;
*(p1 - 1) = *p2 - 5;
*(p2 + 1) = a[2] + a[3];
*(p2 + 2) = *(p1 - 1) + *p1;
printf("%d %d %d %d", a[0], a[1], a[4], a[5]);
}
a[0]= -1 (0.25 point)
a[1]=8 (0.25 point)
a[4]=7 (0.25 point)
a[5]=7 (0.25 point)
Question 5: (2.0 points) Edit bugs of a following program:
The program
#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
void ArrayInput(int *a, int n);
void ArrayPrint(int *a, int n)
void ArraySquare(int *a, int *b, int n);
int main(void)
{
int n;
printf("\n Input element number of array: ");
Edit bugs
#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
void ArrayInput( int *a, int n);
void ArrayPrint(int *a, int n); //0.25
void ArraySquare( int *a, int *b, int n);
int main( void ) {
int n;
printf( "\n Input number of array: " );
Trang 44
scanf("%d", n);
int *a;
int *b;
a = (int *)malloc(n * sizeof(int));
b = &(int *)malloc(n * sizeof(int));
printf("\n Input values of the array\n");
ArrayInput(a, n);
printf("\n Solve square of the array\n");
ArraySquare(a, b, n);
ArrayPrint(a, n);
ArrayPrint(&b, n);
return 0;
}
void ArrayInput(int *a, int n)
{
for (int i = 0; i < n; i++)
{
printf("Input element %d of the array: ", i);
scanf("%d", &a + i);
}
}
void ArrayPrint(int *a, int n)
{
for (int i = 0; i < n; i++)
printf("%d \t", a + i);
printf("\n");
}
void ArraySquare(int *a, int *b, int n)
{
for (int i = 0; i <= n; i++)
{
*(b + i) = *(a + i) *(a + i);
}
}
scanf("%d", &n); //0.25
int *a;
int *b;
a = ( int *)malloc(n * sizeof ( int ));
b = (int *)malloc(n * sizeof(int)); //0.25
printf( "\n Input values of the array\n" );
ArrayInput(a, n);
printf( "\n Sovle square of the array\n" );
ArraySquare(a, b, n);
ArrayPrint(a, n);
ArrayPrint(b, n); //0.25
return 0;
}
void ArrayInput( int * a , int n ) {
for ( int i = 0; i < n ; i++) {
printf( "Input element %d of the array: " , i);
scanf("%d", a + i); //0.25
} }
void ArrayPrint( int * a , int n ) {
for ( int i = 0; i < n ; i++)
printf("%d \t", *(a + i)); //0.25
printf( "\n" );
}
void ArraySquare( int * a , int * b int n ) {
for (int i = 0; i < n; i++) //0.25
{
*(b + i) = *(a + i) * *(a + i); //0.25
}
}
Trang 55
Question 6: (2 points) Write a complete program which executes functions as follows:
a) Create a structure type, named SensorNode, to describe a Sensor Node The Sensor Node will collect temperatures which are used to avoid forest fires, and includes information as follows:
Node Code (named NodeID): integer format
Node Name (named NodeName): string format
Temperature value (named Temp): real number format
b) Enter information including Node Code, Node Name, and Temperature value of 10 different Sensor Nodes
c) Print out a Node Name which collects the highest temperature value
d) Print out the average temperature of these 10 Sensor Nodes
e) Indicates how many Sensor Nodes have temperatures greater than 38.5 Print all information of these Sensor Nodes on the screens
#include "stdafx.h"
#include <stdio.h>
#include <conio.h>
struct SensorNode
{
int NodeID;
char NodeName[32];
float Temp;
void main()
{
int i, dem, k;
float max;
for (i = 0; i < 10; i++) {
printf( "Input Sensor node ID:" );
scanf( "%d" , &p[i].NodeID);
_flushall();
printf( "Input Sensor Name:" );
gets(p[i].NodeName);
printf( "Input temperature of Sensor Node: " );
scanf( "%f" , &p[i].Temp);
max = p[0].Temp;
k = 0;
for (i = 1; i < 10; i++)
if (max < p[i].Temp) {
max = p[i].Temp;
k = i;
printf( "Sensor Name has the highest temperature: " );
Trang 6
6
printf( "%s" , p[k].NodeName); //puts(p[k].tenTr); //0.25
float sum = 0;
for (i = 0; i < 10; i++) {
sum += p[i].Temp;
} printf( "\n The average temperature: %f" , sum/10); //0.25
dem = 0;
for (i = 0; i < 10; i++)
if (p[i].Temp > 38.5)
dem++;
printf( "\n %d Nodes with high temperature (>38.5) \n", dem); //0.25
printf( "Information of those Nodes with high temperature: \n" );
for (i = 0; i < 10; i++)
if (p[i].Temp > 38.5) {
printf( "%d\n" , p[i].NodeID);
puts(p[i].NodeName);
printf( "%f\n" , p[i].Temp);
getch();
}
Notation: Supervisor officials are not allowed to explain the exam questions
Expected Learning Outcomes (About knowledge) Test contents
[ELO 1.3]: Demonstrate command syntax, operation and application of
branching and iteration structures in C language
Questions 1, 2, 3
[ELO 1.5]: Present how the pointer is declared and how to use the
pointer to retrieve the memory
Questions 4, 5
[ELO 1.7]: Define structure type and use structure variables to store and
manage data
Question 6
[ELO 2.1]: Analyze the programming requirements from which to build
a flowchart, a complete program
Questions 1, 2, 6
[ELO 3.1]: Apply control structures, apply data manipulation, build
support functions to design and solve application programming
requirements
Questions 2, 6
June 12th, 2017
Head of ECET