a. void function2( void) { int a, b, result = 0; cin >> a >>b; cout << a+b <<end
ID: 3617790 • Letter: A
Question
a. void function2( void)
{
int a, b, result = 0;
cin >> a >>b;
cout << a+b <<endl;
result = a+b;
return result;
}
b. void f (double a)
{
float a;
cout<< a<<endl;
}
c. void function(int score[])
{
int n;
for (int j = 0; j < n; j ++)
{
cout <<score <<endl;
}
}
d. int sum( int n)
{
if(n==0)
return 0;
else
n+sum(n-1);
}
e. ifstream infile;
infile.open("data.dat");
while (!ifstream.eof( ))
{
// statements here to manipulate data
}
infile.close( );
f. switch(n) {
case 1:
cout << "number is 1" <<endl;
case 2:
cout << "number is 2" <<endl;
break;
default:
cout << "number is 3" <<endl;
break;
}
g. int next;
int sum;
for(int count=1; count <= 10; count++)
{
cin >>next;
sum = sum + next;
}
h. #include <iostream>
using namespace std;
int func_1 (int [], int);
int main()
{
int c[4]={1, 2, 3, 4};
int size = 4;
cout<< func_1(c,size);
return 0;
}
int func_1(int a[], int bound)
{
int sum = 0;
for(int i=0; i< bound;i++)
{
sum=sum+c[i];
}
return sum;
}
Explanation / Answer
a. void function2( void)
{
int a,b, result = 0;
cin>> a >> b;
cout<< a+b << endl;
result= a+b;
returnresult;
}
prototype isvoid.
-----------------------------------------------
b. void f(double a)
{
float a;
cout<< a<< endl;
}
DuplicateIdentifier.
----------------------------------------------
c. void function(intscore[])
{
int n;
for (int j = 0; j< n; j ++)
{
cout << score <<endl;
}
}
n is notinitialized. score is any array,
---------------------------------------------------
d. int sum( intn)
{
if(n==0)
return 0;
else
n+sum(n-1);
}
sum is notdeclare, symbol not found error
-------------------------------------------------------------
e. ifstreaminfile;
infile.open("data.dat");
while (!ifstream.eof())
{
// statements here to manipulatedata
}
infile.close( );
ifstream is not a file, it should beinfile.eof()
----------------------------------------------
f. switch(n){
case 1:
cout <<"number is 1" << endl;
case 2:
cout <<"number is 2" << endl;
break;
default:
cout <<"number is 3" << endl;
break;
}
there is no breakstatement in case 1, so if n is equal to 1, it prints "number is 1"and "number is 2" both. its not an error but its depends upon yourdesired output.
--------------------------------------------------------------------
g. int next;
int sum;
for(int count=1; count <=10; count++)
{
cin >> next;
sum = sum + next;
}
sum should beinitialized because there is by default garbage value init.
---------------------------------------------------------------------
h. #include<iostream>
using namespacestd;
int func_1 (int [],int);
int main()
{
intc[4]={1, 2, 3, 4};
intsize = 4;
cout<< func_1(c, size);
return0;
}
int func_1(int a[], intbound)
{
intsum = 0;
for(int i=0; i< bound; i++)
{
sum=sum+c[i];
}
returnsum;
}
cundeclared,
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.