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."
#include <iostream>
#include <cstdlib>
#include <ctime>
usingnamespace std;
//prototypes
void aras1(int[],int);
void aras2(int[], int);
void aras3(int[],int[], int[]);
constint 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.
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?
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.