turn this array to add into a moltiplication

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
#include <iostream>
using namespace std;

float input(float array[], int num);
float output(float array[], int num);

int main (){
    int x;
    cout << "numbers to add? ";
    cin >> x;
    float red [x];
    
    float input;
      for(int y=0;y<x;y++){
              cin >> input;
              red[y]=input;
              }
              
    float sum=0;
      for(int y=0;y<x;y++){
              sum+=red[y];
              }
      cout << sum << endl;
      

    system("pause");
    return 0;
}

Is there a way to turn this simple array to add into a moltiplication without having to add a second slot like in this case?

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

int main (){
    int x;
    int y;
    int a;
    int b;
    cout << "insert first number ";
    cin >> a;
    cout << "insert second number ";
    cin >> b;
    int asd[a][b];
    for(x=1;x<a;x++){
                      for(y=1;y<b;y++)asd[x][y]=x*y;
                      }
    for(x=1;x<a;x++){
                      for(y=1;y<b;y++)cout << x << "*" << y << "=" << asd[x][y] << " ";
                      cout << "\n";
                      }
    system("pause");
    return 0;
}
I would be surprised if this actually ran, have you tried compiling it? When declaring arrays you need a constant expression (a real number) you cannot use a variable to create an array, because your compiler needs to know the amount of memory it needs to store at compile time.

And the way you want to multiply in your second example no there isn't although if in the case you want to just multiply the two numbers they enter instead of adding them like in the first example you put up you just simply need to change the plus to a multiplication sign.
Topic archived. No new replies allowed.