Hi, I am begginner. I am learning for the exam and i have a few questions. So
1.How many times will the function f in the program fragment be executed?
1 2 3
for ( int i = 0 ; i < n; i++ )
for ( int j = 0; j < n – 1; j++ )
f();
My answer is : n.
2. For what values "i" the lopp does not end?
1 2 3 4 5 6 7 8 9 10
int i = …
while ( i < 10 )
{
if ( i > 5 )
i = i + 1;
elseif ( i < -100 )
i = 5;
else
i = i – 1;
}
3.Correct a mistake
1 2 3
int [] tablica = new tablica[15];
for ( int i = 0; i <= tablica.length; i++ )
tablica[i] = 5;
4.. Complete (in place of dots) the following code to result in the variable x
Had a value equal to the sum of the elements of the array t.
1 2 3 4
double [] t = newdouble[15];
int x = ……..;
for ( int i = 0; i < …………. ; i++ )
x = ……………….;
5.What values will have the variables x and y after execution of the program?
For those kind of complexity it is recommended to learn how to resolve error in your code. You can find a lot of information in this site. If it's too hard for you there programs such as checkmarx that help doing that. I still recommend learning as much as you can and do it on your own.
Good luck.
#include <iostream>
#include <limits>
void waitForEnter();
void f();
int main()
{
std::cout << "Please give me a value for 'n': ";
int n = 0;
std::cin >> n;
std::cin.ignore(1);
// 1.How many times will the function f in the program fragment be executed?
for ( int i = 0 ; i < n; i++ ) {
for ( int j = 0; j < n - 1; j++ ) {
f();
}
}
// 2. For what values "i" the lopp does not end?
int i = …
while ( i < 10 ) // Loop will run until i <= 9
{
if ( i > 5 ) { i = i + 1; } // i will increase if > 5
// so, to ensure i will not become
// more than 9...
elseif ( i < -100 ) { i = 5; } // if i goes under -100, it becomes
// exactly 5 --> 5 is *not* > 5
else { i = i - 1; } // if i is nether > 5 nor < -100,
// it decreases.
}
// 3.Correct a mistake
// Sorry, can't give any help on this code, because there are
// at least three errors.
// Are you sure you have translated the original question correctly?
int [] tablica = new tablica[15];
for ( int i = 0; i <= tablica.length; i++ ) { tablica[i] = 5; }
// 4.. Complete (in place of dots) the following code to result in
// the variable x Had a value equal to the sum of the elements
// of the array t.
double [] t = newdouble[15]; // <-- there's an error here, at the beginning
int x = ..; // if you want to sum all the elements of t[] to 'x',
// 'x' should start with the value...
for ( int i = 0; i < .. ; i++ ) // you need a loop that runs through
// all the elements of t[].
// So it needs to cycle a number of
// times equal to...
x = ..; // At every iteration, one element of t[] should be added to x...
// 5.What values will have the variables x and y after execution of the program?
int x = 7;
int y = 0;
x = y; // now x becomes...
y = x; // if x is now ..., y also becomes ...
waitForEnter();
return 0;
}
void waitForEnter()
{
std::cout << "\nPress ENTER to continue...\n";
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}
void f()
{
staticint invocations = 1;
std::cout << "invocations of f(): " << invocations++ << "; ";
}
2) don't think so ... if 9 > 5 add 1, 10 isn't less than 10, it stops. Looks like anything less than 6 gets stuck, to me. Its governed by the addition statement; if it cant increment it cant reach 10. Anything less than 5 is eventually set back to 5, over and over....