Solving Array Homework: Output Explained

Also note that expr1 is evaluated only once, at the beginning of the loop.In summary, the given code uses a for loop to iterate through an array in reverse order, starting at the index of the given value b and ending at 0. If the value at the current index is negative, it is output as a positive number. The order of the output is in reverse, starting from the 4th index to the 0th index.
  • #1
asz304
108
0

Homework Statement


say a[5] = { 5.6, -3.2, 11, -7.7, -1 );
and b = 5
Code:
void fum ( double a[], int b ){
 for ( int i = b -1; i >= 0; i-- ){
    if ( a[i] < 0 )
        cout << -a[i];
    else cout << a[i]; 
    cout << ", ";
       }
}

Why is the output

1, 7.7, 11, 3.2, 5.6

and not

7.7, 11, 3.2, 5.6 ?

Shouldn't the value of the first i in the for loop 3? and not 4? because i = 5-1 = 4 then it decrements to 3?
 
Physics news on Phys.org
  • #2
Your index will decrement after the loop has iterated once. So the first run through the index will be (5-1)=4, then decremented to 3 on the next iteration.

Since the 4th item in your array is -1, it'll print that (0-based index, of course).
 
  • #3
Thanks. :D
 
  • #4
A for loop generally looks like this:
Code:
for(expr1; expr2; expr3)
{
   statement(s)
}
The order in which things happen is:
1. expr1 is evaluated.
2. expr2 is evaluated. If true (non-zero), the body of the for loop is executed. If false (zero), control flows to the first statement following the for loop.
3. expr3 is evaluated.

Note that when expr2 evaluates to false (zero), step 3 is not performed.
 
  • #5


The output is in fact 7.7, 11, 3.2, 5.6, 1. This is because the for loop starts at the index of b-1, which in this case is 4. It then decrements i until it reaches 0, which means it will loop through the indices 4, 3, 2, 1, and 0. Therefore, the output will be the values at indices 4, 3, 2, 1, and 0 in that order. The first value, 1, is the value at index 4, which is -1 after being multiplied by -1 in the if statement. The value of i itself does not correspond to the indices being accessed in the array, it simply helps keep track of the loop iteration.
 

1. What is an array?

An array is a data structure that stores multiple values of the same data type in a sequential manner. It is a useful tool for organizing and manipulating data in a program.

2. How do you declare an array in most programming languages?

In most programming languages, an array can be declared by specifying the data type and the number of elements within square brackets (e.g. int array[5]). Some languages also allow dynamic arrays, where the size can be changed during runtime.

3. How do you access elements in an array?

To access elements in an array, you need to use the index number of the element. In most programming languages, arrays start at index 0, so the first element would be accessed using array[0].

4. How do you solve array homework problems?

To solve array homework problems, it is important to understand the problem and the desired output. Then, you can use loops and conditional statements to manipulate the array elements and produce the desired output.

5. What are some common errors when dealing with arrays?

Some common errors when dealing with arrays include accessing elements outside the bounds of the array, forgetting to initialize the array, and not using the correct data type for the elements in the array. It is important to carefully check the code and debug any errors to ensure the array is being used correctly.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
2
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
714
  • Engineering and Comp Sci Homework Help
Replies
24
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
8
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
18
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
21
Views
2K
  • Programming and Computer Science
Replies
15
Views
1K
Back
Top