CPP Last Assignment
CPP Last Assignment
Chapters
Questions and
Exercises
هحود حسن علي حسن:الباحث
Chapter 1
1- A compiled language runs more quickly than an interpreted one because the
compiler translates each program statement into machine language.
A. never
B. only once
2- After die source file for a C program has been written, it must
be c---------------, l---------------, and e---------------.
Answer: Compiled, linked, and executed
3- The library files that come with the C programming system contain.
4- The executable files that come with the C programming system include a program
to a--------------- the different phases of the compilation process.
Answer: Automate
5- What is the purpose of the parentheses following the word main in a C program?
Answer: The parentheses following main() indicate that “main” is a function and provide a
place to put arguments.
7- True or False: a carriage return must be used at the end of every C program
statement.
void main
(
Print”Oh, woe and suffering!”
)
Answer: Here's what's wrong with the example:
9- What two sorts of things can go in the parentheses following the function printf()?
Answer: On the left there is a string which will be printed; on the right is a series of constants
(or variables) which will be placed in the string.
void main(void)
{
Printf(“%s\n%s\n% s”,”one”,”two”,”three”);
}
Answer: The output is:
One
Two
Three
هحود حسن علي حسن:الباحث
Chapter 2
1. Declaring variables is advantageous because it
C--------------
i--------------- f--------------- l--------------- d---------------
-
Answer: Character (char) integer (int) floating point (float)
long integer (long) double-precision floating point (double)
A. int a;
B. float b;
C. double float c;
D. unsigned char d;
Answer: A, B, and C are correct.
هحود حسن علي حسن:الباحث
Answer: True
5. Type float occupies --------------- times as many bytes of memory as type char.
Answer: Four
A. num==2;
B. int num;
C. num <2;
D. int num==2;
Answer: D
8. True or False: type long variables can hold numbers only twice as big as type int
variables.
Answer: False: it can hold numbers 65,536 times as large.
هحود حسن علي حسن:الباحث
Answer: '\x41' prints 'A' and '\xE0' prints the Greek letter alpha.
A. 1.000.000
B. 0.000.001
C. 199.95
D. -888.88
Answer: Decimal exponential
A. 1.000.000 1.0e6
B. 0.000.001 1.0e—6
C. 199.95 1.9995e2
D. -888.88 -8.8888e2
هحود حسن علي حسن:الباحث
A. 1.5e6
B. 1.5e—6
C. 7.6543e3
D. -7.6543e—3
Answer: exponential Decimal
A. 1.5e6 1.500.000
B. 1.5e—6 0.000,001,5
C. 7.6543e3 7,654.3
D. -7.6543e—3 0.007,654,3
/* age.c */
/* caLcuLates age in days */
void main(void)
{
float years, days;
printf(“please type your age in years: “); /* print prompt*/
scanf (“%f”, years); /* get input */
days = years * 365; /* find answer*/
printf ("You are %f days old.\n", days); /* print answer*/
}
Answer: There's no address operator preceding the years variable in the scanf() function.
هحود حسن علي حسن:الباحث
Answer: True
A. +
B. &
C. %
D. <
Answer: A and C
Answer: '\t' is the tab character, '\r' is the return character which prints a carriage return (but
no linefeed), and not to be confused with the newline character, '\n', which prints
both a carriage return and a linefeed.
A. a single character
21. True or False: You need to press [Return] after typing a character in order for
getche() to read it.
Answer: False
هحود حسن علي حسن:الباحث
A. combine values
B. compare values
A. 1 > 2
B. „a‟ <‟b‟
C. 1 == 2
D. „2‟==‟2‟
Answer: A. 1>2 False
B. „a‟ < „b‟ True
C. 1 == 2 False
D. „2‟ == „2‟ True (the ASCII codes are compared)
هحود حسن علي حسن:الباحث
A. is most important
B. is used first
C. is fastest
/* This is a
/* comment which
/* extends over several lines */
Answer: No. The begin comment symbol (/*) can‟t be used within a comment
هحود حسن علي حسن:الباحث
Chapter 3
1. The three parts of the loop expression in a for loop are
A. right bracket
B. right brace
C. Comma
D. semicolon
Answer: D
3. A _______ is used to separate the three parts of the loop expression in a for loop.
Answer: Semicolon
هحود حسن علي حسن:الباحث
A. right bracket
B. right brace
C. comma
D. semicolon
Answer: B
7. True or false: the initialize expression and increment expression are contained in
the loop expression in awhile loop.
Answer: False
هحود حسن علي حسن:الباحث
9. The more deeply a loop is nested, the more _______it should be indented.
Answer: Deeply
10. An assignment statement itself can have a______________* just like a variable.
Answer: Value
11. The advantage of putting complete assignment statements inside loop expressions is
12. True or false: in almost every case where a variable can be used, an increment or
decrement operator can be added to the variable.
Answer: True
14. The break statement is used to exit from which part of a loop?
A. beginning
B. middle
C. end
Chapter 4
1. In a simple if statement with no else, what happens if the condition following the if
is false?
A. the program searches for the last else in the program
B. nothing
3. True or false: nesting one if inside another should be avoided for clarity.
Answer: False
هحود حسن علي حسن:الباحث
7. True or false: the compiler interprets else-if differently than it does an equivalent if-
else.
Answer: False
8. The statements following a particular else-if in an else-if ladder are executed when
the conditional expression following the else-If is true and all previous
A.
conditions are true
the conditional expression following the else-if is true and all previous
B.
conditions are false
the conditional expression following the else-If is false and all previous
C.
conditions are true
the conditional expression following the else-If is false and all previous
D.
conditions are false
Answer: B
D. the last If not enclosed in braces and not matched with its own else
Answer: D
هحود حسن علي حسن:الباحث
12. True or false: a break statement must be used following the statements for each case
in a switch statement.
Answer: False: break statements are not used if two cases trigger the same set of
statements.
هحود حسن علي حسن:الباحث
Chapter 5
1. Which of these are valid reasons for using functions?
2. True or false: a function can still be useful even if you can't pass it any information
and can't get any information back from it.
Answer: True
3. Is this a correct call to the function abs(), which takes one argument?
ans = abs(num)
Answer: No: the call to the function must be terminated with a semicolon.
4. True or false: to return from a function you must use the keyword return.
Answer: False: you can return from a function by simply 'falling through" the closing
brace.
هحود حسن علي حسن:الباحث
5. True or false: you can return as many data items as you like from a function to the
calling program, using the keyword return()
Answer: False: you can only return one data item with a function using retur().
8. True or false: the variables commonly used in C functions are accessible to all other
functions.
Answer: False: functions commonly use local variables which are accessible only to the
function in which they're defined.
9. Which of the following are valid reasons for using arguments in functions?
A. Constants
C. preprocessor directives
{
Int three=3;
type (three);
}
void type(float num)
}
printf (“%f”,num);
}
Answer: No: the argument passed to the function is type hit in the calling program but
type float in the function.
Answer: Many
هحود حسن علي حسن:الباحث
A. in main() only
A. in main() only
17. The #define directive causes one phrase to be ________________ for another.
Answer: Substituted
19. In this #define directive, which is the identifier and which is the text?
#define EXP 2.71828
Answer: "EXP" is the identifier and "2.71828" is the text.
21. A variable should not be used to store values that never change because
22. Will the following code correctly calculate postage that is equal to a fixed rate times
the sum of the combined girth and height of a parcel?
#define SUM3(Length,width,height) Length + width + height
----------
postage = rate * SUM3(l,w,h)
Answer: No: The macro expands into the incorrect
Postage = rate * L+W+H
23. The #include directive causes one source file to be ________________ in another.
Answer: Inserted
هحود حسن علي حسن:الباحث
Answer: Include
هحود حسن علي حسن:الباحث
Chapter 6
1. An array is a collection of variables of
10. What will happen if you try to put so many variables into an array when you
initialize it that the size of the array is exceeded?
A. nothing
11. What will happen if you put too few elements in an array when you initialize it?
A. nothing
13. What will happen if you assign a value to an element of an array whose subscript
exceeds the size of the array?
A. the element will be set to 0
15. In the array in the question immediately above, what is the name of the array
variable with the value 4?
Answer: Array[1][0]
17. If you don't initialize a static array, what will the elements be set to?
A. 0
B. an undetermined value
18. When you pass an array as an argument to a function, what is actually passed?
20. A string is
A. a list of characters
B. a collection of characters
C. an array of characters
D. an exaltation of characters
Answer: C
B. a string array
C. a string constant
D. a string of characters
Answer: C
هحود حسن علي حسن:الباحث
24. The function _________ is designed specifically to read in on string from the
keyboard.
Answer: gets()
and you type in a string to this array, the string can consist of a maximum of
______________ characters.
Answer: 9 (space must be left for the ‘\0’ character)
26. True or false: the function puts() always adds a‟ \n‟ to the end of the string it is
printing.
Answer: True
هحود حسن علي حسن:الباحث
A. gets()
B. printf()
C. scanf()
D. puts()
Answer: A
30. What expression would you use to find the length of the string name?
Answer: strlen(name)
الباحث :هحود حسن علي حسن
C Programs
هحود حسن علي حسن:الباحث
C Report
Q1. Write a C program to compute the average score of N student and also find the
maximum score and its index.
Answer.
#include <stdio.h>
#include <conio.h>
main()
{
/* declaring variables */
int i,n,index;
float scores, max, sum, ave ;
max = 0;
}
/* adding student score to the sum of scores */
sum = sum + scores;
}
getche();
}
هحود حسن علي حسن:الباحث
Q2. Write a C program to compute the average of a set of scores, stop reading if
score<0.
Answer.
#include <stdio.h>
#include <conio.h>
main()
{
/* declaring variables */
int i, n ;
float scores,sum, ave ;
sum =0 ;
getche () ;
}
الباحث :هحود حسن علي حسن
Q3. Write a C program to find the maximum and the smallest number from N
values.
Answer.
#include <stdio.h>
#include <conio.h>
main()
{
/* declaring variables */
int n,i;
float max,min,number;
{
if (number>=max) max= number;
if (number<min) min= number;
}
}
/* printing the maximum and minimum values */
printf("Maximum value is: %f Minimum Value is: %f", max, min);
getche();
}
هحود حسن علي حسن:الباحث
Q4. Write a C program to find the maximum and the smallest number from N
values.
Answer.
#include <stdio.h>
#include <conio.h>
main()
{
/* declaring variables */
int i, n, count;
float scores,sum,avg;
count = 0 ;
/* getting number of students */
printf("Enter number of Students = ");
scanf("%d", &n);
Answer.
#include <stdio.h>
#include <conio.h>
main()
{
/* declaring variables */
int i, id, countM, countF;
char gender;
countM = 0;
countF = 0;
/* for loop to student ID and “M or F” */
getche();
}
الباحث :هحود حسن علي حسن
)(main
{
هحود حسن علي حسن:الباحث
/* declaring variables */
int row , column ;
Q7. Write a C program to compute the letter grade of students, read student name,
and compute the letter grade according to the following:
Grade A B C D E F
90- 80- 70- 60- 50-
Score <50
100 90 80 70 60
Answer.
#include <stdio.h>
#include <conio.h>
main()
{
/* declaring variables */
int i, n, score;
char Sname[255];
main()
{
float mark, marktasks, midterm, finalexam, tscore, fulltasks, fullmidterm,
fullfinal;
getch();
}
هحود حسن علي حسن:الباحث
main()
{
// declaring variables
float mark, marktasks, midterm, finalexam, tscore, fulltasks, fullmidterm,
fullfinal;
int i,n;
/* getting number of students */
printf ("Enter number of Students = " );
scanf ( "%d", &n) ;
fullmidterm=(mark*30)/100;
fullfinal=(mark*40)/100;
getch();
}
main()
{
// declaring variables
float mark, marktasks, midterm, finalexam, tscore, fulltasks, fullmidterm,
fullfinal;
هحود حسن علي حسن:الباحث
int i,n,excellent,vgood,good,bad,fail;
excellent=vgood=good=bad=fail=0;
;)(getch
}
هحود حسن علي حسن:الباحث
main()
{
/* declaring variables */
int i,n;
float G, H, R, TAX, NET, totalG, totalTAX, totalNET;
totalG = totalTAX = totalNET = 0;
char name[255];
/* getting hour pay rate */
printf ("Enter hour pay rate: ");
scanf ("%f", &R);
/* getting number of employees */
printf ("\nEnter number of employees: ");
scanf ("%d", &n);
/* for loop to get employees data */
هحود حسن علي حسن:الباحث
if (G<1000) TAX = 0;
else if (G<3000) TAX = 0.05 * G;
else if (G<5000) TAX = 0.07 * G;
else TAX = 0.1 * G;
printf ("\nTAX for this employee is: %.2f", TAX);
totalTAX = totalTAX + TAX;
NET = G - TAX;
printf ("\nNet Salary for this employee is: %.2f", NET);
printf ("\n");
totalNET = totalNET + NET;
}
/* Printing Totals */
printf ("\nTotal Growth Pay is: %.2f", totalG);
printf ("\nTotal TAX is: %.2f", totalTAX);
printf ("\nTotal Net Salary is: %.2f", totalNET);
getche();
}
هحود حسن علي حسن:الباحث
main()
{
int n, m, b, c, a[100], i, x, count;
char d[15];
count = I =0;
d[10]='a';
d[11]='b';
d[12]='c';
d[13]='d';
d[14]='e';
d[15]='f';
هحود حسن علي حسن:الباحث
while (n>b)
{
m=n/b;
n=n-(m*b);
a[i] = n;
i++;
count++;
n=m ;
}
if (count!=0)
a[ i ] = m;
else
a[ i ] = n;
;)(getche
}
هحود حسن علي حسن:الباحث
Q13. Write a program to calculate the new price for TV , after discount as
follows :
Size discount
12 5%
16 10%
18 15%
Other no discount
Use switch statement
Answer.
#include <stdio.h>
#include <conio.h>
main()
{
float price;
int size;
switch(size)
{
case 12: printf("TV price after discount is : %f", price-(price*5)/100);
break;
case 16: printf("TV price after discount is : %f", price-(price*10)/100);
break;
الباحث :هحود حسن علي حسن
Arrays Exercises
Q1. Write a program to compute the average of week‟s temperature using array.
Answer.
#include <stdio.h>
#include <conio.h>
main()
{
int temper[7]; //array declaration
int day, sum;
sum = 0;
getche();
}
هحود حسن علي حسن:الباحث
Answer.
#include <stdio.h>
#include <conio.h>
main()
{
int i,n;
i=0;
getche();
}
هحود حسن علي حسن:الباحث
No Answer
Q4. Write a program to read and write two dimensional array of integer
numbers.
Answer.
#include <stdio.h>
#include <conio.h>
main ()
{
int TRows, TCols;
int row, col;
}
;)" printf("\nThe entered array is:
)for (row = 0 ; row < TRows ; row++
{
;)"printf("\n
)for (col = 0 ; col < TCols ; col++
;)]printf("%d\t", newarray[row][col
}
;)(getche
}
هحود حسن علي حسن:الباحث
Q5. Write a program to read the scores of 6 students in 5 subjects into array, and
then calculate the average of scores for each student.
No Answer.
)(b. main
{
;float x=5.5, y=4
;int a=9, b=4
;)printf(“\n%d”, (int)x + (int)y
;)printf(“\n%d”, a/b
;)printf(“\n%d”, a%b
;)printf(“\n%f”, (float)a/b
}
Answer.
#include <stdio.h>
#include <conio.h>
main ()
{
float F,P,i,n;
F = P * pow(1+i,n);
printf("\nValue of F = %f", F);
getche();
}