Review Mid2 C Questions Model Answer
Review Mid2 C Questions Model Answer
#include <iostream>
using namespace std;
int myfunc(int);
int myfunc(float);
int main(){
cout << myfunc(3) << "\n";
return 0;}
int myfunc(int n){
return n * 2;}
int myfunc(float n){
return n * 3.0;}
a- 7 b- 7.02 c- 6 d- 10.53
#include <iostream>
using namespace std;
int sub1(int &n){
n--;
return n;}
int main(){
int m = 10;
int j=5;
m -= sub1(j);
cout << m << "\n";
return 0;}
a- 0 b- 1 c- 6 d-10
#include <iostream>
using namespace std;
int f(int &i){
i = 10;
return(5 * i);}
int main(){
int n = 5;
cout << f(n) <<"-";
cout << n << "\n";
return 0;}
#include <iostream>
using namespace std;
double sum(int pt[], int n){
int temp = 0;
for (int i = 0; i < n; ++i) {
temp += pt[i];}
return temp;}
int main(){
int total;
int pt[5];
for (int i = 0; i < 5; i++)
pt[i] = i;
total = sum(pt, 3);
cout << total << endl; return 0;}
TA.mona Al-humoud
a- 3 5 b- 3 c- 6 5 d- 10 3
#include <iostream>
using namespace std;
int fun1();
int fun2(int);
int fun3(int , int);
int fun4(int &);
int main(){
int i = 5;
cout << fun1() ;
cout << fun2(3) ;
cout << fun3(6,2) ;
cout << fun4(i) ;
return 0;}
int fun1( ){
int y = 3 ;
return y*y;}
int fun2(int x ){
return x+x ;}
int fun3(int x , int y){
return x / y ;}
int fun4(int &x ){
return x-x ;}
int x = 0 * (1 / 3 * 3.0);
cout << x << endl;
a- 0 b- 0.0 c- 1 d-1.0
{
int i = 1;
double x = 1.111;
cout << i << " " << x << "\n";
{
int x = 2;
double i = 2.222;
cout << i << " " << x << "\n";
}
}
a-1 1.111 2.222 2 b-1.111 1 2.222 2 c-2.222 2 1 1.111 d-1.111 1 2 2.222
Find Errors
1-
int y=0;
while (y>0) {
cout<<"nice day";
y++;}
cout<<"Good bye";
2-
int y=1;
while (y>0)
{cout<<"nice day";
y++;}
cout<<"Good bye";
TA.mona Al-humoud
3-
int counter = 0;
while (counter <= 10)
cout<<"counter= "<<counter;
answer : Logical error (Infinite loop), since there is no update for the counter
4-
for (int counter=1; counter<=5; counter++)
cout<<counter<<endl;
cout<<counter+1<<endl;
answer : Syntax error counter is declared in the for and cannot be used outside its
body.
Which of these function calls correct for the functions bellow ( more then one )
1- double GetHours(double Mon, double Tue , double Wed, double Thu , double Fri) {
double TotalHours = Mon + Tue + Wed + Thu + Fri;
return TotalHours;
}
a- GetHours( 3.5,1.5,2.5,1.5,4.5)
b- GetHours()
c- GetHours(‘a’,’b’,’c’,’d’,4)
d- GetHours(3,4,4,5,1.5)
e- GetHours(3.5,4,’a’,true,1.5)
a- sum( 5 , 7 )
b- sum( 6.3 )
c- sum( 7 , 3.5 , ‘a’ )
d- sum( )
TA.mona Al-humoud
#include<iostream>
using std:: cout;
using std:: endl;
The output:
int main()
{int x=3,y=4; &&&
for (int i=1; i<=y; i++)
{for (int j=1; j<=x; j++)
cout<<'&'; &&&
cout<<endl; }
for (int i=1; i<=x; i++)
&&&
for (int j=1; j<=i; j++)
cout<<'*';
return 0;}
void main( )
{
int a,b;
The output:
a=2, b=0;
while (a*b<=24) a= 2, b= 3
{
b=a+1;
cout<<"a= "<<a<<", b= "<<b<<'\n'; a=3, b=4
++a;
b+=2;
}}
a=4, b=5
#include <iostream>
using namespace std;
int main(){
int n1=10, n2, n3;
n2=n1+10; n3=n1+20;
for(; n1<n3 ;n1+=2)
{
if(n1==n2 || n1==(n2+4) ) The output:
continue;
cout<<n1<<"\t"; 10 12 14 16 18 22 26
} return 0;} 28
#include <iostream>
using namespace std ;
double average(double , double , double );
int main()
{
double number1 = 4 , number2 = 5 , number3 = 7 ;
double average_result = average(number1, number2, number3);
cout<<"the average of three numbers you entered is "<<average_result;
return 0;
}
double average(double num1, double num2, double num3)
{
double avg;
avg = (num1 + num2 + num3) / 3;
return avg;
} The output:
TA.mona Al-humoud
Write a function named "digit_name" that takes an integer argument in the range from 1 to 9 ,inclusive, and
prints the English name for that integer on the computer screen. No newline character should be sent to the
screen following the digit name. The function should not return a value. The cursor should remain on the
same line as the name that has been printed. If the argument is not in the required range, then the function
should print "digit error" without the quotation marks but followed by the newline character. Thus, for
example,
the statement digit_name(7); should print seven on the screen;
the statement digit_name(0); should print digit error on the screen and place
the cursor at the beginning of the next line.
Answer:
Answer:
Write a function named "enough" that takes one integer argument, call it "goal" and returns as its value the
smallest positive integer n for which 1+2+3+. . . +n is at least equal to goal . Thus, for example,
cout <<enough(9)<<endl; // will print 4 because 1+2+3+4 _ 9 but 1+2+3<9
cout <<enough(21)<<endl;// will print 6 'cause 1+2+ . . .+6 _ 21 but 1+2+ . . . 5<21
cout <<enough(-7)<<endl;//will print 1 because 1 _ 7 and 1 is the smallest
Answer:
int enough (int goal)
{
int n = 1, sum = 1;
while (sum < goal)
sum += ++n; return n; }
TA.mona Al-humoud
A positive integer n is said to be prime (or, "a prime") ( ) العدد األوليif and only if n is greater than 1
and is divisible only by 1 and n . For example, the integers 17 and 29 are prime, but 1 and 38 are not prime.
Write a function named "is_prime" that takes a positive integer argument and returns as its value the integer 1
if the argument is prime and returns the integer 0 otherwise. Thus, for example,
cout << is_prime(19) << endl; // will print 1
cout << is_prime(1) << endl; // will print 0
cout << is_prime(51) << endl; // will print 0
Answer :
Write a function named "sum" that takes as its arguments the following:
(1) an array of floating point values;
(2) an integer that tells how many floating point values are in the array.
The function should return as its value the sum of the floating point values in the array. Thus, for example, if
the array that's passed to the function looks like this:
5.8 | 2.6 | 9.0 | 3.4 | 7.1
The function should return 27.9 as its value .
Answer:
TA.mona Al-humoud
Answer:
Write a function named "location_of_largest" that takes as its arguments the following:
(1) an array of integer values;
(2) an integer that tells how many integer values are in the array.
The function should return as its value the subscript of the cell containing the largest of the values in the
array.Thus, for example, if the array that's passed to the function looks like this:
58 | 26 | 90 | 34 | 71
then the function should return the integer 2 as its value. If there is more than one cell containing the largest
of the values in the array, then the function should return the smallest of the subscripts of the cells containing
the largest values.
Answer:
TA.mona Al-humoud
write a program that calculates the squares and cubes of the integers from 0 to n and uses
tabs to print the following neatly formatted table of values:
#include <iostream>
#include <cmath> // to use pow function
1 1 1
2 4 8
3 9 27
4 16 64
5 25 125
6 36 216
counter=7
TA.mona Al-humoud