[try Beta version]
Not logged in

 
 
C++ arrays and array parameter - BUG!

Jan 16, 2017 at 7:59am


This code has a bug! (Bugs on lines 4 and 21 according to online compiler C++ shell.)

Purpose of this program: I am trying to cin inside an array and pass in that array to another function to do some multiplication and then print out the values . Please take a look at this code.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
using namespace std;

void tripler (int n[], size){
    
    for (int i = 0; i < size; i++){
        n[i] = n[i] * i;
        cout << n[i];
    }
}

int main (){
    

    int a[3];
    for (int i = 0; i < 3; i++){
    
    cin >> a[i];
    int size = 0;
    size += i;
    tripler (a[], size);
    }
    return 0;
}

Last edited on Jan 16, 2017 at 8:34am
Jan 16, 2017 at 8:36am
A possible solution I found, but I want to know what others can think of. How can we do it using extra parameter size?

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

#include <iostream>
using namespace std;

void tripler (int n[]){
     for (int j = 1; j <= 3; j++){
    for (int i = 0; i < 3; i++){
        n[i] *=  j;
        cout << n[i]<<",";
    }
     
    cout<<endl;
     }
}

int main (){
    

    int a[3];
    for (int i = 0; i < 3; i++){
    
    cin >> a[i];
  
    
    }
    tripler (a);
    return 0;
}



creds: ASNM - SOLOLEARN


Jan 16, 2017 at 8:37am
Kourosh23, the problem with your code is that a unknown sized array (in your code, n[]) can't be passed as a parameter of a method. Try to pass a size-known array or a pointer to the 0th element instead.

You can consult to "The C++ Programming Language" Ch7.4 for a better explanation.
Jan 16, 2017 at 8:55am
Yeah I am learning how to use combination of arrays and pointers. Thanks for the reference :)
Jan 16, 2017 at 9:17am
How can we do it using extra parameter size?
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
#include <iostream>
using namespace std;

void tripler (int n[], size){
    
    for (int i = 0; i < size; i++){
        n[i] = n[i] * i;
        cout << n[i];
    }
}

int main (){
    

    int a[3];
    int size = 0; // Note: size needs to be defined outside the loop because it needs to be used outside the loop
    for (int i = 0; i < 3; i++){
    
    cin >> a[i];
    int size = 0;
    size += i; // Note: += i is wrong
    ++size;
    tripler (a[], size);
    }
    tripler (a, size); // Note: outside the loop/[] are wrong here
    return 0;
}
Jan 16, 2017 at 9:30am
closed account (1vRz3TCk)
what is the type of size?...
void tripler (int n[], (?) size){
Jan 17, 2017 at 3:35am
@coder777, yes your notes were helpful, and I was able to come up with this code.

@CodeMonkey, yes I made a mistake. Size must have the type int when it is passed as a parameter to a function. Good point.

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


#include <iostream>
using namespace std;

void tripler  (int a[], int size){
    cout << "Tripled elements: ";

    /* This for loop will loop through all the elements of the array and times them by
   three which will print out array elements with 3 times the original value. */
    for (int i = 0; i < size; i ++){
        a[i] *= 3;
        cout << a[i] << " ";
    }
}

int main (){
    int size = 6;
    int a[size];
    cout << "Enter "<< size << " elements: ";
    for (int i = 0; i < size; i++){
        cin >> a[i];
    }
    
    tripler (a, size);
    
    
    return 0;
}



EXTRA INFORMATION: PROGRAM WILL CRASH

This code contains const int a[] as a parameter which checks if the array a[] changes throughout the program or no, and since we are doing a[i] *= 3; or a[i] = a[i] * 3; Then we are changing the original value of the array, thus the program crashes. It is all because of the addition of const to the parameter array.

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

#include <iostream>
using namespace std;

void tripler  (const int a[], int size){
    cout << "Tripled elements: ";
    for (int i = 0; i < size; i ++){
        a[i] *= 3;                 // Will show as bug! We are changing array.
        cout << a[i] << " ";
    }
}

int main (){
    int size = 6;
    int a[size];
    cout << "Enter "<< size << " elements: ";
    for (int i = 0; i < size; i++){
        cin >> a[i];
    }
    
    tripler (a, size);
    
    
    return 0;
}
Last edited on Jan 17, 2017 at 7:29pm
Jan 17, 2017 at 6:40pm
Just a note:
You should use code comments to explain what you expect to happen. For example, there should be a comment block above tripler() that helps to explain:
1) What you expect the result of tripler() to be.
2) What the calling function should pass to tripler()

Getting into this habit now will help you write code that is easier to debug and maintain.


Jan 17, 2017 at 7:21pm
@moonman239 , yes, I definitely will.
Topic archived. No new replies allowed.