A few questions

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;
 else if ( 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 = new double[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?
1
2
3
4
int x = 7;
int y = 0;
x= y;
y = x;

I think, that x=7
Thank you very muchy.
1) No. the second loop is in the first one. The first one goes 0-n-1, or n times, and for each of those the second loop goes ... try again.

5) you need values for both x and y. Ignore line 4. What is x? Now if x is that value, line 4 sets y to what?

I can't tell that you even tried the others?

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.
Hints:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#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...

        else if ( 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 = new double[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()
{
    static int invocations = 1;
    std::cout << "invocations of f(): " << invocations++ << "; ";
}

So, my answers :
1. n(n-1)
2.9
4.15
5.0
Is that correct?
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....

4 has multiple answers. One of them is 15.

5) both are zero at the end. correct.
Last edited on
Topic archived. No new replies allowed.