P: 26

i've been trying to figure out these questions and i can't seem to understand them...can anyone help?
1)Write a full program in C++ that creates the twodimensional randomArray
array with 10 rows and 10 columns, and fills it with random numbers
respectively: the first row with random number in range [1, 100), the second
row with random number in range [100, 200), …, the 10th row with random
number in range [900, 1000), The program then displays the array elements
onscreen 10 in each row.
2) Create two arrays with 10 elements averageRows and averageColums.
Calculate the average value for each row of randomArray and assign them
respectively to elements of averageRows array. (The average of the first row
of randomArray to the first element of averageRows, … the average of the
last row of randomArray to the last element of averageRows). Fill in the
same way the averageColums array but this time with average values of
columns of randomArray. Calculate the average of the diagonal of array
averageRows (e00, e11, …, e99) and the average of elements of the second
diagonal of averageRows (e90, e81, …, e09). Display all the results on screen.
thx in advance ;)
 
Share this Question
Expert Mod 2.5K+
P: 4,677

i've been trying to figure out these questions and i can't seem to understand them...can anyone help?
1)Write a full program in C++ that creates the twodimensional randomArray
array with 10 rows and 10 columns, and fills it with random numbers
respectively: the first row with random number in range [1, 100), the second
row with random number in range [100, 200), …, the 10th row with random
number in range [900, 1000), The program then displays the array elements
onscreen 10 in each row.
2) Create two arrays with 10 elements averageRows and averageColums.
Calculate the average value for each row of randomArray and assign them
respectively to elements of averageRows array. (The average of the first row
of randomArray to the first element of averageRows, … the average of the
last row of randomArray to the last element of averageRows). Fill in the
same way the averageColums array but this time with average values of
columns of randomArray. Calculate the average of the diagonal of array
averageRows (e00, e11, …, e99) and the average of elements of the second
diagonal of averageRows (e90, e81, …, e09). Display all the results on screen.
thx in advance ;)
What don't you understand? Is it the data creation, the manipulation...?
 
P: 26

both of them...its so frustrating...btw...i'm a rookie to programming
  Expert Mod 2.5K+
P: 4,677

both of them...its so frustrating...btw...i'm a rookie to programming
Ok, so you need help with the algorithm  let's take them one at a time.
Can you tell me the steps you would take to solve the first one, without a computer?
 
P: 26

basically..make 10 rows with random numbers in them starting in the 1100 range on the first row....100,200 on da second and so on til the 10th row....i really suck at this
  Expert Mod 2.5K+
P: 4,677

basically..make 10 rows with random numbers in them starting in the 1100 range on the first row....100,200 on da second and so on til the 10th row....i really suck at this
Nah, you're just starting out. Nobody's amazing when they start out  but we can get you there ;)
So  to refine your algorithm a bit:
create array[10][10]
for each i in array[1][i]
input random number between 1 and 100
for each i in array[2][i]
input random number between 100 and 200
for each i in array[3][i]
input random number between 200 and 300
for each i in array[4][i]
input random number between 300 and 400
and so on until 10
does that look right?
If so, how familiar are you with the rand() function?
 
P: 26

yea it looks right...and yea i'm a bit familiar wit ran()..etc :)
  Expert Mod 2.5K+
P: 4,677

yea it looks right...and yea i'm a bit familiar wit ran()..etc :)
Ok, so are you starting to see how (from the algorithm) this is going to come together?
I'm sure you can create the headers and main declaration  then you just need to follow your algorithm.
 
P: 26

Ok, so are you starting to see how (from the algorithm) this is going to come together?
I'm sure you can create the headers and main declaration  then you just need to follow your algorithm.
yea i'm starting to see it :)
  Expert Mod 2.5K+
P: 4,677

yea i'm starting to see it :)
Ok, so you have your basic structure, and your arrays declared, right?
Then you just need to figure out how to get the random number in between 1 and 100.
I think this link will give you a good idea of how to do that  but definitely post if you get confused.
(Anyone else have a good link to show this?)
 
P: 26

Ok, so you have your basic structure, and your arrays declared, right?
Then you just need to figure out how to get the random number in between 1 and 100.
I think this link will give you a good idea of how to do that  but definitely post if you get confused.
(Anyone else have a good link to show this?)
yea i'm startting to understand this..a bit...i'm a slow learner too
  Expert Mod 2.5K+
P: 4,677

yea i'm startting to understand this..a bit...i'm a slow learner too
Hey, as long as you're learning. I just made a jackass of myself in another thread. It's all about keeping with it and learning something from it.
  Expert Mod 2.5K+
P: 4,677

Posted from users PM:
so far this is wut i got... 
#include <iostream>

using namespace std;


int main()

{

int rand[10];

int i;

for ( i = 1; i < 10; i++)

cin >> random[i]=1;

is this right so far?
You're getting the idea, but there are a few comment's I'd like to make.
1) naming variables. Once you get into larger programs, it's good practice to name variables with descriptive names. You did this, but you are also going to be using the rand() function to get the random numbers. This is going to make these two confusing. I would try 'columns' or 'matrix' or something a bit more unique, but still descriptive of what it is.
2) you declare rand and then try to access random. The method of access is correct (you use i as the index correctly), but the variable name isn't correct  this will cause an error.
3) you have the user entering in the values for the array. You will want to declare rand and assign it to a number (that will get overwritten ever time you call another random number), and then set that equal to the array.
4) This might be unintentional, but it's good that you started small  only did one part of the doublefor loop, 2d array first, and then when you understand it, it'll be very simple to make it bigger.
5) You declared 'int main()' and so should end with a 'return 0; }' (The '}' should be on the next line, I just don't know how to format it off the top of my head, without using the [ code] tags)
 
P: 26

Posted from users PM:
You're getting the idea, but there are a few comment's I'd like to make.
1) naming variables. Once you get into larger programs, it's good practice to name variables with descriptive names. You did this, but you are also going to be using the rand() function to get the random numbers. This is going to make these two confusing. I would try 'columns' or 'matrix' or something a bit more unique, but still descriptive of what it is.
2) you declare rand and then try to access random. The method of access is correct (you use i as the index correctly), but the variable name isn't correct  this will cause an error.
3) you have the user entering in the values for the array. You will want to declare rand and assign it to a number (that will get overwritten ever time you call another random number), and then set that equal to the array.
4) This might be unintentional, but it's good that you started small  only did one part of the doublefor loop, 2d array first, and then when you understand it, it'll be very simple to make it bigger.
5) You declared 'int main()' and so should end with a 'return 0; }' (The '}' should be on the next line, I just don't know how to format it off the top of my head, without using the [ code] tags)
but still just trying to write it is frustrating.....wut i do is write it on paper then type it out....i keep getting messed up...how would u write it? if i don't mind asking
  Expert Mod 2.5K+
P: 4,677

but still just trying to write it is frustrating.....wut i do is write it on paper then type it out....i keep getting messed up...how would u write it? if i don't mind asking
Well, I'd start out with the shell: 
#include <iostream>

using namespace std;


int main()

{


return 0;

}

and then I would put my algorithm inside 
#include <iostream>

using namespace std;


int main()

{

// create matrix[10][10]

// for each i in matrix [1][i]

// get random number between 1 and 100

// matrix[1][i] = random number

// for each i in matrix [2][i]

// get random number betwee 100 and 200

// matrix[2][i] = rand

/// and on to matrix [10][i]

return 0;

}

Then I would go through and start breaking that algorithm down  into statements that could be in a single line, and then try to turn it into code 
#include <iostream>

using namespace std;


int main()

{

// create matrix[10][10]

int matrix[10][10];

int tmp_rand;

// for each i in matrix [1][i]

for (int i = 0; i < 10; i++) {

// get random number between 1 and 100

/* I'm not going to give this away, but I'm pretty sure you could figure it out */

// matrix[1][i] = random number

// for each i in matrix [2][i]

// get random number betwee 100 and 200

// matrix[2][i] = rand

/// and on to matrix [10][i]

}

return 0;

}

and so on. This way it starts out as a working template (even though it doesn't do anything, it will compile), and builds up from there  and my code will be commented!
Does that make sense? Help at all?
 
P: 26

Well, I'd start out with the shell: 
#include <iostream>

using namespace std;


int main()

{


return 0;

}

and then I would put my algorithm inside 
#include <iostream>

using namespace std;


int main()

{

// create matrix[10][10]

// for each i in matrix [1][i]

// get random number between 1 and 100

// matrix[1][i] = random number

// for each i in matrix [2][i]

// get random number betwee 100 and 200

// matrix[2][i] = rand

/// and on to matrix [10][i]

return 0;

}

Then I would go through and start breaking that algorithm down  into statements that could be in a single line, and then try to turn it into code 
#include <iostream>

using namespace std;


int main()

{

// create matrix[10][10]

int matrix[10][10];

int tmp_rand;

// for each i in matrix [1][i]

for (int i = 0; i < 10; i++) {

// get random number between 1 and 100

/* I'm not going to give this away, but I'm pretty sure you could figure it out */

// matrix[1][i] = random number

// for each i in matrix [2][i]

// get random number betwee 100 and 200

// matrix[2][i] = rand

/// and on to matrix [10][i]

}

return 0;

}

and so on. This way it starts out as a working template (even though it doesn't do anything, it will compile), and builds up from there  and my code will be commented!
Does that make sense? Help at all?
yea it does make a bit sense...it does help...thx dawg (thumbs up)....but i'll still ask for help though :)
  Expert Mod 2.5K+
P: 4,677

yea it does make a bit sense...it does help...thx dawg (thumbs up)....but i'll still ask for help though :)
Absolutely  we're more than happy to help.
And, just in case you didn't see (if you didn't you shouldn't worry about it  I'm sure it will come to you when you start programming), there is another optimization you can make in the algorithm.
 
P: 26

Absolutely  we're more than happy to help.
And, just in case you didn't see (if you didn't you shouldn't worry about it  I'm sure it will come to you when you start programming), there is another optimization you can make in the algorithm.
really? like what?
  Expert Mod 2.5K+
P: 4,677

really? like what?
Well, at least in my algorithm (as yours only deals with half first). Just think of ways to keep yourself from having to write more code.
 
P: 26

Well, at least in my algorithm (as yours only deals with half first). Just think of ways to keep yourself from having to write more code.
ok that sounds good...btw...i'm starting to understand the program u wrote...thx again ;)
  Expert Mod 2.5K+
P: 4,677

ok that sounds good...btw...i'm starting to understand the program u wrote...thx again ;)
Good, that was the point. And by the way  I didn't write it  you are going to. You did come up with the algorithm and most of the code. You know what you're doing, you just don't think you do yet.
Definitely post again if you get stuck!
 
P: 26

ok ...i was going over this plus reading other things on arrays and etc which then i got confused and this is wut i came up with so far
btw...if u don't mind i used your variables
#include <iostream>
using namespace std;
int main()
{
int matrix[10][10]
int tmp_rand;
for (int i = 0; i < 10; i++);
{ matrix[1][i] = (1 < i < 100);
  Expert Mod 2.5K+
P: 4,677

ok ...i was going over this plus reading other things on arrays and etc which then i got confused and this is wut i came up with so far
btw...if u don't mind i used your variables
#include <iostream>
using namespace std;
int main()
{
int matrix[10][10]
int tmp_rand;
for (int i = 0; i < 10; i++);
{ matrix[1][i] = (1 < i < 100);
I dont' mind at all. Um, ok, I think you're trying to do too much at once. Let's break that up into two steps:
1) get random number between 1 and 100
2) assign number to matrix[][]
How would you do the first?
 
P: 26

I dont' mind at all. Um, ok, I think you're trying to do too much at once. Let's break that up into two steps:
1) get random number between 1 and 100
2) assign number to matrix[][]
How would you do the first?
the first part i remember from math that if a number is between two numbers ...i assume thats wut u do in c++ so i put it there (shrugs my shoulders)
but the matrix thing its coming to me now
  Expert Mod 2.5K+
P: 4,677

the first part i remember from math that if a number is between two numbers ...i assume thats wut u do in c++ so i put it there (shrugs my shoulders)
but the matrix thing its coming to me now
Good!
Well, here I'm also assuming you can get a random number in the first place  let's start there, how would you do that?
 
P: 26

Good!
Well, here I'm also assuming you can get a random number in the first place  let's start there, how would you do that?
ihave no clue ...at first i thought i knew it but i don't sorry :(..i really suck at this
  Expert Mod 2.5K+
P: 4,677

ihave no clue ...at first i thought i knew it but i don't sorry :(..i really suck at this
Ah! Ok, sorry, that was my fault, I was trying to push you to do something you didn't know. It's ok, check this link for rand() . That should start to clear up a lot more.
 
P: 26

Ah! Ok, sorry, that was my fault, I was trying to push you to do something you didn't know. It's ok, check this link for rand() . That should start to clear up a lot more.
ok its begining to look ok...so how would i input this in the program?
  Expert Mod 2.5K+
P: 4,677

ok its begining to look ok...so how would i input this in the program?
Well, that's what I'm asking you  there are examples in the article. Just worry about putting in one. How would you assign a random number from rand() to tmp_rand?
 
P: 26

Well, that's what I'm asking you  there are examples in the article. Just worry about putting in one. How would you assign a random number from rand() to tmp_rand?
?? (shrugs my shoulders)
  Expert Mod 2.5K+
P: 4,677

?? (shrugs my shoulders)
14th line in their example.
 
P: 26

14th line in their example.
14th line?
  Expert Mod 2.5K+
P: 4,677

14th line?
In the example code of the link I posted.
  Expert Mod 2.5K+
P: 4,677

In the example code of the link I posted.
You would assign it the way you assign anything. As long as the library (stdlib) is included, you can just assign a random number to any other number (say tmp_rand).
 
P: 26

In the example code of the link I posted.
u mean "rand() % 10 + 1;"?
  Expert Mod 2.5K+
P: 4,677

u mean "rand() % 10 + 1;"?
Well, I was talking more how they assign it.
variable = rand() % 10 + 1;
gives variable (I dont' remember what they used) a random value.
The mod (%) 10 is what you're going to be interested in next  this returns a number between 0 and 10. So you just have to change that to give you one between 1 and 100.
 
P: 26

Well, I was talking more how they assign it.
variable = rand() % 10 + 1;
gives variable (I dont' remember what they used) a random value.
The mod (%) 10 is what you're going to be interested in next  this returns a number between 0 and 10. So you just have to change that to give you one between 1 and 100.
oooo ok....i see
    Question stats  viewed: 1507
 replies: 36
 date asked: Feb 20 '07
