3. Correct the errors in the following: ( 4 points ) // assume correct includes
ID: 3536518 • Letter: 3
Question
3. Correct the errors in the following: (4 points)
// assume correct includes
int main()
{
int num1, num2, sum;
char again;
while ( toupper( again ) == 'Y' )
{
cout << "Enter a number: ";
cin >> num1;
cout << "Enter another number: ";
cin >> num2;
cout << "Their sum is " << ( num1 + num2 ) << endl;
sum += num1 + num2;
cout << "Another time? -> ";
cin >> again;
}
cout << "The sum of all the values is "
<< sum << endl;
return 0;
}
Explanation / Answer
part 1 : If arr represents an array, by arr[] ==>then arr represent the base addresss of the array that is the address of the first element of the array.
so arr++ should not be used in the program.
part 2: if size of string is x and assuming char size = 2 byte
total size required for the structure = 2*2*x+4+8 bytes
in question (3)
the sum is not initialized to zero hence every time the previous value of sum will be carried
e.g. if first time ==>num1=5 ; num2 =10 then ==>sum = 15
second time==>num1=5 ; num2 =10 then ==>sum = 30
so we have to add the statement
sum=0; just in the begining of while loop.
part4 : in the declaration of pointer variable the type is also declared that is what type of data is pointed by this pointer
char *p;int *p;==>on this basis system determine how many bytes to use when retrieving the value being pointed to
in the program
struct quest
{
int * ptr;
char c;
};
// elsewhere in the program
quest q;
q.ptr = 7;
q.c = "4";
the error q.ptr = 7;
q.c = "4";
instead of this we should use q->ptr=7; q.c = '4';
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.