Arrays

I have an assignment that says "Write a program that puts random numbers in the range 0 - 9 into two separate arrays and stores the product of the parallel elements of the two arrays in a third array. Output will show the contents of all three arrays."


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
#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

//prototypes
void aras1(int[],int);
void aras2(int[], int);
void aras3(int[],int[], int[]);

const int SIZE = 10;

int main()
{
    srand((unsigned)time(NULL));
    int num_ara1[SIZE];
    int num_ara2[SIZE];
    int product[SIZE];

    aras1(num_ara1, SIZE);
    aras2(num_ara2, SIZE);
    aras3(product,num_ara1, num_ara2);

    return 0;
}
void aras1(int numRand1[], int)
{
    for (int i = 0; i < SIZE; i++)
    {
        numRand1[i] = static_cast<int>(1+ rand() % 10);

        cout << 1+ rand() % 10 << '\t';
    }
    cout << endl;

    return;
}

void aras2(int numRand2[], int)
{
    for (int i = 0; i < SIZE; i++)
    {
        numRand2[i] = static_cast<int>(1+ rand() % 10);

        cout << 1+ rand() % 10 << '\t';
    }
    cout << endl;

    return;
}

void aras3(int product[], int numRand1[], int numRand2[])
{
    for (int i = 0; i < SIZE; i++)
    {
    if(product[i] == numRand1[i])
        cout << numRand1[i] * numRand2[i];
    }

     return;
}


I feel I am close yet so far. I do not fully understand arrays. my teacher does not explain it well. the only thing I really need to do is multiply each integer in array 1 by integers in array 2 and show product.
Here is a good explanation:
http://www.cplusplus.com/doc/tutorial/arrays/
Last edited on
Note that your functions aras1() and aras2() are identical.
You would get the same result, if your main() would call:
1
2
    aras1( num_ara1, SIZE );
    aras1( num_ara2, SIZE );

Two calls to same function. Both perform the same operations, but on different data.

What is the purpose of the second parameter in:
void aras1( int[], int );
We can see that the function does not use it for anything.


The function aras1 performs two operations:
* Assign random values to elements of an array
* Show random values

Overall, it is better for a function to do only one thing.
Here it is worse, for the shown values have no relation to the stored values.


What does the test on line 57 represent?

You don't "store product to array" anywhere. "Store" means "assign".
for each k
  C[k] = A[k] + B[k]

you know I really don't know what I was doing. I watched a bunch of the teachers videos and lectures and notes. honestly this was a lot of trial and error and I don't even really know how to implement stuff. I vaguely understand what an array is used for however I lack the ability to implement it. So to answer all your question quite frankly I do not know. I just plugged stuff in until it ran with no error which it has thus far.
The tutorial that nuderobmonkey did point to does have a section Arrays as parameters. An example in it takes array and int as parameters, and explains it too.


If you do write (or copy&paste):
1
2
3
4
5
for ( int i = 0; i < SIZE; i++ ) {
  if ( product[i] == numRand1[i] )   {
    cout << numRand1[i] * numRand2[i];
  }
}

then you must have some idea in your mind.
Even if you had not, can you tell what that code does?

This might help: http://www.cplusplus.com/doc/tutorial/control/
Dude this is a question for the beginners section
Sure. but lets help him a bit anyway.
First, I have to advise against just grabbing random code and beating it into submission. That is ONE way to learn, and it works really well once you have the basics down, but its going to be exceedingly painful to learn that way at your level. You need to read some background before you go there.

Arrays are used to store a bunch of variables of the same type. Its that simple.
instead of
int a,b,c,d,e,f,g,h,i,j,k,l,m,... ; //ever tried to track this much junk in code? its very ugly.
you can have
int array[100]; //100 variables, nice and neat.


and you can access it from 0 - size, here in my 100 example, from 0 to 99 or for(x = 0; x < 100; x++) works great.
array[x] = x; //boring, but its an example.

its up to your code to track which array location you need and when; most of the time you have one of a few things going on... you can compute the index, and directly fetch the one you want, you iterate all of them, each in turn, you have a related entity (like user input) that tells you which one you want, you randomly pick one (say if your array means a deck of cards), and a few other similar ideas.

vector is a direct upgrade to arrays. Arrays are crude, and are really just a chunk of raw memory on your machine. Vectors have some tools that make them much easier to work with.
Last edited on
Registered users can post here. Sign in or register to post.