C programming Question solve

profileKate leon
Cprogram.pdf

C programming

1

1161 Question 1

(a) Discuss the differences between a directed graph and an undirected graph. (b) Perform a depth first search on the following graph starting from vertex u.

u v w

x y z

(c) List four out of the six phases that a C program needs to go through for it to be executed

(9 marks)

Question 2

Write a program that utilises looping to produce the following table of values:

N N-3 N+3 N*3

9 6 12 27

18 15 21 54

27 24 30 81

36 33 39 108

45 42 48 135

(6 marks)

Question 3

Write a program that calculates and prints the sum of the multiples of 3 from 1 to 45.

(6 marks)

Question 4

Write a program that creates an array storing the following 6 numbers:

23.4, 55, 22.6, 3, 40.5, 18

and calculates the average of these numbers.

(6 marks)

C programming

2

Question 5

(a) Write a program to concatenate two strings using standard library function struct(). (b) Write a program to concatenate two strings manually without using strcat() function.

(7 marks)

Question 6

(a) What does this program do? What does the program print?

#include <stdio.h>

int main() {

int *p , num;

p = &num;

*p = 100;

printf("%d\n" , num);

(*p)++;

printf("%d\n" , num);

(*p)--;

printf("%d\n" , num);

}

(b) What does this program do? What does the program print?

#include <stdio.h>

int main() {

int balance;

int *address;

int value;

balance = 5000;

address = &balance;

value = *address;

printf("Balance is : %d\n" , value);

}

(6 marks)

C programming

3

Question 7

What does this program do? What does the program print?

#include <stdio.h>

#include <string.h>

void guess2(char *a);

int main(){

char a[ 20 ];

printf( "Enter the strings:\n ");

gets(a);

guess2(a);

return 0;

}

void guess2(char *a){

char strev[20];

char *m = a;

char *n = strev;

while(*m!='\0'){

m++;

}

m--;

while(m>=&a[0]){

*n++ = *m--;

}

*n = '\0';

printf("string = %s", strev);

}

(4 marks)

Question 8

Write a program to create a structure(student) which contains name, roll and marks as its data

member. Then, the program creates a structure variable(s). Then, data (name, roll and marks)

is taken from user and stored in data members of structure variable s. Finally, the data entered

by user is displayed.

(6 marks)

C programming

4

1172 Question 1

(c) Discuss the differences between a directed graph and an undirected graph. (d) In most cases, a for statement in a c program can be represented with an equivalent while statement. However, there is one exception. Discuss this exception. (e) Describe the technique to generate a different sequence of random numbers each time the program runs.

(9 marks)

Question 2

Write a program that utilises looping to produce the following table of values:

N N-3 N+3 N*3

9 6 12 27

18 15 21 54

27 24 30 81

36 33 39 108

45 42 48 135 (6 marks)

Question 3

Assume that array int v[5] has been defined and its first element is at location 3000 in memory.

(d) How to initialise a pointer variable vPtr to point to v[0]? (e) What is the memory address that the pointer vPtr points to after executing the following statement vPtr += 3;? Assume that an integer is stored in 4 bytes of memory.

(4 marks)

Question 4

What will the output be when you execute the following c code?

#include<stdio.h>

int main(){

const int *p;

int a=10;

p=&a;

printf("%d",*p);

return 0;

} (4 marks)

C programming

5

Question 5

Write a c program that contains a function stringConcat to concatenate two strings without using string functions of the standard library.

(7 marks)

Question 6

What will the output be of following program? Assume that the memory addresses for i, j and k are 1000, 2000 and 3000 respectively.

#include<stdio.h>

int main(){

int i = 3;

int *j;

int **k;

j=&i;

k=&j;

printf("%u %u %d ",k,*k,**k);

return 0;

} (5 marks)

Question 7

Create a structure with members int a, int b and float c. Write a program that demonstrates how a structure variable value1 can be assigned to another structure variable value2. Print the value assigned to the structure variable value2.

(5 marks)

Question 8

Write a program to create a structure student, which contains name, roll and marks as its data

member. Then, the program creates a structure variable s. Then, data name, roll and marks is taken from the user and stored in data members of structure variable s. Finally, the data entered

by the user is displayed. (6 marks)

Question 9

What is the output of the following program?

#include <stdio.h>

#include <ctype.h>

void convert ( char *sPtr );

int main( void )

{

char string[] = "characters and $32.98";

C programming

6

printf( "The string before is: %s", string );

convert( string );

printf( "\nThe string after is: %s\n", string );

}

void convert( char *sPtr ) {

sPtr+=10;

while ( *sPtr != '\0' ) {

*sPtr = toupper( *sPtr );

++sPtr;

}

} (4 marks)

C programming

7

1183 Question 1

(a) Explain the meaning of “pass-by-reference” and “pass-by-value” in C programming. (b) List all the edges in the following multigraph.

(c) Perform a breadth first search starting from vertex b.

a b c d

e f g h

(10 marks)

Question 2

In this question, we address the flow of oil through a pipeline. Friction between the liquid and the wall of the circular pipe causes a velocity profile to develop in the flowing oil. Oil which is in contact with the walls of the pipe is not moving at all, while oil at the centre of the flow has the highest velocity. The diagram below gives an idea of how the velocity of the oil varies across the diameter of the pipe and defines the variables used in this analysis. The following equation describes the velocity profile:

v(r) = Vmax(1-r/r0) 0.2

where r0 = 1.83 m and Vmax = 1.25 m/s

C programming

8

(a) Draw a flowchart of the function main( ) of a program that computes the velocity, v(r), in

the pipeline at a certain position. The program should ask the user to enter the radius r (in m)

from the keyboard. The computation of the velocity v(r) should be done in the function profile(

) which is called from main( ). The result should be displayed on the screen by function main( )

and the user should then be asked to enter y or n to indicate whether he/she wants to do another

calculation. If y is entered, the program should calculate the velocity from another entry,

otherwise the program should stop. If a radius is selected by the user, which is larger than r0

(outside the pipe), the program should warn the user and ask them to select a smaller value. (b) Write the program main( ) based on the flow chart in the question above. For the function profile( ), as described in the question above, use radius and vel only as variables in the function profile( ). DO NOT declare any additional variables.

(20 marks)

Question 3

Write a program that calculates and prints the product of the even integers from 4 to 20.

(10 marks)

Question 4

Write statements to accomplish the following:

(a) Define matrix to be an integer array and to have 2 rows and 2 columns. Assume the

symbolic constant LENGTH has been defined to be 2. (b) Use a for repetition statement to initialize each element of matrix to the product of its

subscripts. Assume the integer variables a and b are defined as control variables. (c) Write the statements to print the values of each element of array matrix. Assume the

matrix was initialized with the definition: int matrix [LENGTH][ LENGTH ] ={ { 1 }, { }

};

(12 marks)

Question 5

What is the output of the following program if the user enters “awesome program”? Explain your answer.

#include <stdio.h>

void AFunction();

C programming

9

int main()

{

printf("Enter a sentence: ");

AFunction();

return 0;

}

void AFunction()

{

char c;

scanf("%c", &c);

if( c != '\n')

{

AFunction();

printf("%c",c);

}

} (9 marks)

Question 6

Write a program that generates 4 random numbers. Each random number is within the range of 1

to 13. (12 marks)

Question 7

The following program passes the variable number by value to the function cubeByValue.

C programming

10

Write a program that performs the same function as the above program but the variable number is passed by reference to the function cubeByValue. Hint: Use pointer.

(12 marks)

Question 8

Assume that integer array b[5] and integer pointer variable bPtr have been defined. Write a statement to set bPtr equal to the address of the first element in array b. Write a statement using pointer expression to reference the array element b[3].

(6 marks)

Question 9

Provide the definition for each of the following structures:

(a) Structure time containing integers hours, minutes and seconds. (b) A structure called month that contains character arrays monthName[30], daysOfWeek[20], and an integer variable numberOfDays.

(9 marks)

C programming

11

1194 Question 1

(c) Explain the meaning of “pass-by-reference” and “pass-by-value” in C programming. (d) In most cases, a for statement in a C program can be represented with an equivalent while

statement. However, there is one exception. Discuss this exception. (e) Describe the technique to generate a different sequence of random numbers each time the program

runs. (9 marks)

Question 2

Write a C program that takes an arithmetic operator +, -, *, / and two operands from the user and

performs the calculation on the two operands depending upon the operator entered by the user, using

switch and break statements.

(6 marks)

Question 3

Write a C program to add numbers until user enters zero, using a do … while loop. (4 marks)

Question 4

What will the output be when you execute the following C code?

#include<stdio.h> int main(){

const int *p; int a=10; p=&a;

printf("%d",*p); return 0;

} (4 marks)

Question 5

Write a C program that asks user to enter a string and a character and checks how many times the

character is repeated in the string.

(5 marks)

C programming

12

Question 6

Write a program that takes three integers entered by a user and stores them in variable a, b and c

respectively. Then, these variables are passed to a function using pass by reference. This function swaps

the value of these elements in cyclic order. The program prints the output on the screen. Be low is the

sample output of the program:

Enter value of a, b and c respectively: 1 2 3 Value before swapping:

a=1

b=2 c=3

Value after swapping numbers in cycle:

a=3

b=1

c=2 (6 marks)

Question 7

Write a C program to store the information (name, roll and marks) of 10 students using structures. In the

program, a structure, student is created. This structure has three members: name (string), roll (integer)

and marks (float). Then, a structure array o f size 10 to store information of 10 students is created.

Using for loop, the program takes the information of 10 students from the user and displays it on the

screen. Below is an example of program output:

Enter information of students:

For roll number 1, Enter name: Tom Enter marks: 98

For roll number 2, Enter name: Jerry Enter marks: 89 . . . Displaying Information:

Roll number: 1 Name: Tom Marks: 98 . .

.

(6 marks)

C programming

13

Question 8

Write a C program to calculate the power of a number using recursion. (6 marks)

Question 9

Assume that integer array b[5] and integer pointer variable bPtr have been defined. Write a statement to

set bPtr equal to the address of the first element in array b. Write a statement using pointer expression

to reference the array element b[3].

(4 marks)

finish