By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,918 Members | 2,246 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,918 IT Pros & Developers. It's quick & easy.

need help plz

Bigj0410
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 two-dimensional 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
on-screen 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 ;)
Feb 20 '07 #1
Share this Question
Share on Google+
36 Replies


sicarie
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 two-dimensional 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
on-screen 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...?
Feb 20 '07 #2

Bigj0410
P: 26
both of them...its so frustrating...btw...i'm a rookie to programming
Feb 20 '07 #3

sicarie
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?
Feb 20 '07 #4

Bigj0410
P: 26
basically..make 10 rows with random numbers in them starting in the 1-100 range on the first row....100,200 on da second and so on til the 10th row....i really suck at this
Feb 20 '07 #5

sicarie
Expert Mod 2.5K+
P: 4,677
basically..make 10 rows with random numbers in them starting in the 1-100 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?
Feb 20 '07 #6

Bigj0410
P: 26
yea it looks right...and yea i'm a bit familiar wit ran()..etc :)
Feb 20 '07 #7

sicarie
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.
Feb 20 '07 #8

Bigj0410
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 :)
Feb 20 '07 #9

sicarie
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?)
Feb 20 '07 #10

Bigj0410
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
Feb 20 '07 #11

sicarie
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.
Feb 20 '07 #12

sicarie
Expert Mod 2.5K+
P: 4,677
Posted from users PM:
so far this is wut i got...
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int main()
  5. {
  6. int rand[10];
  7. int i;
  8. for ( i = 1; i < 10; i++)
  9. cin >> random[i]=1;
  10.  
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 double-for loop, 2-d 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)
Feb 20 '07 #13

Bigj0410
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 double-for loop, 2-d 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
Feb 20 '07 #14

sicarie
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:

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int main()
  5. {
  6.  
  7.     return 0;
  8. }
  9.  
and then I would put my algorithm inside


Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int main()
  5. {
  6.     //  create matrix[10][10]
  7.     // for each i in matrix [1][i]
  8.         // get random number between 1 and 100
  9.         // matrix[1][i] = random number
  10.     // for each i in matrix [2][i]
  11.         // get random number betwee 100 and 200
  12.         // matrix[2][i] = rand
  13.     /// and on to matrix [10][i]
  14.     return 0;
  15. }
  16.  
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


Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int main()
  5. {
  6.     //  create matrix[10][10]
  7.     int matrix[10][10];
  8.     int tmp_rand;
  9.     // for each i in matrix [1][i]
  10.     for (int i = 0; i < 10; i++) {
  11.         // get random number between 1 and 100
  12.         /* I'm not going to give this away, but I'm pretty sure you could figure it out */
  13.         // matrix[1][i] = random number
  14.     // for each i in matrix [2][i]
  15.         // get random number betwee 100 and 200
  16.         // matrix[2][i] = rand
  17.     /// and on to matrix [10][i]
  18.     }
  19.     return 0;
  20. }
  21.  
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?
Feb 20 '07 #15

Bigj0410
P: 26
Well, I'd start out with the shell:

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int main()
  5. {
  6.  
  7.     return 0;
  8. }
  9.  
and then I would put my algorithm inside


Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int main()
  5. {
  6.     //  create matrix[10][10]
  7.     // for each i in matrix [1][i]
  8.         // get random number between 1 and 100
  9.         // matrix[1][i] = random number
  10.     // for each i in matrix [2][i]
  11.         // get random number betwee 100 and 200
  12.         // matrix[2][i] = rand
  13.     /// and on to matrix [10][i]
  14.     return 0;
  15. }
  16.  
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


Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int main()
  5. {
  6.     //  create matrix[10][10]
  7.     int matrix[10][10];
  8.     int tmp_rand;
  9.     // for each i in matrix [1][i]
  10.     for (int i = 0; i < 10; i++) {
  11.         // get random number between 1 and 100
  12.         /* I'm not going to give this away, but I'm pretty sure you could figure it out */
  13.         // matrix[1][i] = random number
  14.     // for each i in matrix [2][i]
  15.         // get random number betwee 100 and 200
  16.         // matrix[2][i] = rand
  17.     /// and on to matrix [10][i]
  18.     }
  19.     return 0;
  20. }
  21.  
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 :)
Feb 20 '07 #16

sicarie
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.
Feb 20 '07 #17

Bigj0410
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?
Feb 20 '07 #18

sicarie
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.
Feb 20 '07 #19

Bigj0410
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 ;)
Feb 20 '07 #20

sicarie
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!
Feb 20 '07 #21

Bigj0410
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);
Feb 20 '07 #22

sicarie
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?
Feb 20 '07 #23

Bigj0410
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
Feb 20 '07 #24

sicarie
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?
Feb 20 '07 #25

Bigj0410
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
Feb 20 '07 #26

sicarie
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.
Feb 20 '07 #27

Bigj0410
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?
Feb 20 '07 #28

sicarie
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?
Feb 20 '07 #29

Bigj0410
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)
Feb 20 '07 #30

sicarie
Expert Mod 2.5K+
P: 4,677
?? (shrugs my shoulders)
14th line in their example.
Feb 20 '07 #31

Bigj0410
P: 26
14th line in their example.
14th line?
Feb 20 '07 #32

sicarie
Expert Mod 2.5K+
P: 4,677
14th line?
In the example code of the link I posted.
Feb 20 '07 #33

sicarie
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).
Feb 20 '07 #34

Bigj0410
P: 26
In the example code of the link I posted.
u mean "rand() % 10 + 1;"?
Feb 20 '07 #35

sicarie
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.
Feb 20 '07 #36

Bigj0410
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
Feb 20 '07 #37

Post your reply

Sign in to post your reply or Sign up for a free account.