Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 5

Exercise 1.

i. What do you think is the error in this code?

ii. Fix the error


it do?

Answer i: What is wrong with the above code is the omission of the “#” before
include in the pre-processor before the header file.
Answer ii: To fix this program we have to add the # before the include l.e
#include<iostream>
And what the program does is to display “Hello My Country Guys” on the
screen as shown before;

Exercise 2
i. What’s wrong with this variable name? int integer = 0;
ii. Why should you not use #define to declare a constant?
iii. Why would you initialize a variable?
Answers
i. There’s absolutely nothing wrong with declaring a variable name as
integer although it main cause confusion to misinterpret int(keyword)
as integer variable name.
ii. While it is possible to use the #define preprocessor directive to declare
a constant in C++, there are several reasons why it is generally
considered to be a less desirable approach than using a const
declaration or an enumeration:
i. No type checking: When you use #define to declare a constant,
there is no type checking. This means that the compiler cannot
catch errors in which you use the constant with the wrong data
type, which can lead to hard-to-debug errors in your code.
ii. No scope control: #define constants are not scoped in the same
way as const variables or enumerations. This means that they
are available globally in your program, which can lead to
naming conflicts and unintended side effects.
iii. Debugging issues: #define constants are replaced by the pre-
processor with their literal values, which can make debugging
more difficult because the actual value of the constant may not
be visible in the source code.
iii. why would you initialize a variable?
To Avoiding undefined behavior: If you don't initialize a variable before
using it, its initial value is undefined. This means that the variable could
contain any value, including garbage values, and using it could lead to
undefined behavior. Initializing a variable to a known value avoids this
problem.
Exercise 3
1. What is wrong with this code fragment?
Int MyNumbers[5] = {0};
MyNumbers[5] = 450; // setting the 5th element to value 450.
2. What is wrong with this code fragment?
Int MyNumbers[5];
Cout<<MyNumbers[3];

Answers.
1. Int MyNumbers[5]={0};
MyNumbers[5]=450;
Array start indexing from 0-4 which comprises of 5 element in an array
the initialization of array done in this code fragment is 5 element which
will index from 0-4 and the next line is calling the 5 th value this will
either return an error or return 0 depending on the compiler type.

2. Int MyNumber [5];


Cout<<MyNumbers[3];
What is wrong with this code fragment is that the array is not initialized
or an array is with size 5 is been declared without assigning or identifying
a specific element in the array and calling the MyNumbers[3] will return
either an error or zero(0) depending on the compiler type.
Exercise 4
1. Write a for loop to access elements in an array in the reverse order.
2. What is wrong with this code?
int loopCounter = 0;
while(loopCounter < 5);
{
cout<<loopCounter<<" ";
loopCounter++;
}

Answers
1. A for Loop code to access elements in a reverse order and it outout

2. What is wrong with this code


int loopCounter = 0;
while(loopCounter < 5);
{
cout<<loopCounter<<" ";
loopCounter++;
}
Is that the semicolon at the end of this code while(loopCounter < 5);
is breaking the statement apart, and ,making it meaningless it is a logical
error.
Exercise 5
Write a function with return type void that still helps the caller calculate the area
and circumference of a circle when supplied the radius.
Answer:

Exercise 6
1. Write a program to calculate the area and circumference of a circle where
the radius is fed by the user.
Answer:

Answer No.2 if the output of this code is to be store in integer that mean we
will not get an actual value of the answer them the Area will be 78 instead of
78.5398 and the circumference would be 31 instead of 31.4159.
3. Int MyNumber [5];
Cout<<MyNumbers[3];
What is wrong with this code fragment is that the array is not initialized or an
array is with size 5 is been declared without assigning or identifying a specific
element in the array and calling the MyNumbers[3] will return either an error or
zero(0) depending on the compiler type.
4. What is wrong with this code
int loopCounter = 0;
while(loopCounter < 5);
{
cout<<loopCounter<<" ";
loopCounter++;
}
Is that the semicolon at the end of this code while(loopCounter < 5);is breaking the
statement apart, and ,making it meaningless it is a logical error.

You might also like