i have big problem rand()

Pages: 12
hi my name is Elvis and i am building a program that it has to be submitted today and i cannot find the way yo finish it. the program is this::

1. If all 3 numbers in any given row, column, or diagonal (8 possibilities in all) are all odd (i.e.,any combination of only Hearts and Clubs), a score of 100 is computed for that one row, column or diagonal. Examples of these 8 possibilities are shown below:






no1 no2 no3
no4 no5 no6
no7 no8 no9
3 horizontal rows


no1 no2 no3
no4 no5 no6
no7 no8 no9
3 vertical columns


no1 no3
no5
no7 no9
2 diagonals

Note: You will need to write a mathematical equation for each row, column, and diagonal (8 equations in all) that will compute the score for that row, column and diagonal. You can accomplish this without using any branching statements.



2. Compute the total score by adding up all 8 individual scores computed for each row, column diagonal in step 1.

NOW MY PROBLEM IS THAT I GET THE RANDOM NUMBERS AND EVERYTHING BUT THE EQUATION THAT I NEED TO BUILD TO MAKE ODDS BE 100 IT IS WITHOUT IF STATEMENT, THE PROFESSOR SAID THAT WITH MODULUS % WE SHOULD BE ABLE TO DO IT. CAN SOMEBODY HELP ME THANK YOU...
i think i dont understand what has it to do with modals but try this
if n=3,x=5
y[10][10];
int k=0;
for(i=0;i<n;i++)
for(j=0;j<n;j++)
{
y[i][j]=k;
k++;
}
for(i=0;i<n;i++)
for(j=0;j<n;j++)
{
if(i==j||i+j==n-1)
cout<<y[i][j];
}
ofcourse this is not a complete code but the others are includes and simple things
ok i could not understand what you just wrote here is the code so far:


#include <iostream>
#include <ctime>
#include <cstdlib>

using namespace std;

int main()
{
//declaring variables
int seed = 0; //Declaration for seed
int no1= 0;
int no2= 0;
int no3= 0;
int no4= 0;
int no5= 0;
int no6= 0;
int no7= 0;
int no8= 0;
int no9= 0;



//Set the seed to be the number of seconds since 1970
seed = time(0);

//Initialize srand function with seed
srand(seed);


//limits the random number
no1 = rand() % 4 + 3;
no2 = rand() % 4 + 3;
no3 = rand() % 4 + 3;
no4 = rand() % 4 + 3;
no5 = rand() % 4 + 3;
no6 = rand() % 4 + 3;
no7 = rand() % 4 + 3;
no8 = rand() % 4 + 3;
no9 = rand() % 4 + 3;


//make the char of the clubs, hearts, diamond and spaids
cout << (char)no1 << "----------" << (char)no2 << "----------" << (char)no3 << endl;
cout << "|" << " " << "|" << " " << "|" << endl;
cout << (char)no4 << "----------" << (char)no5 << "----------" << (char)no6 << endl;
cout << "|" << " " << "|" << " " << "|" << endl;
cout << (char)no7 << "----------" << (char)no8 << "----------" << (char)no9 << endl;



return 0;
}


so i have everything right what i cannot do is the equation that makes that if one of the 3 rows is all odds it is going to be 100 this is the picture of the example that he made...


http://www.mypicx.com/09202010/hearts/
Perhaps you should explain that again... preferably from the beginning.
I don't see what there is to get about any random numbers - they were never mentioned.
Edit: well, the code more or less explains it.

And what on earth is
no1 no3
no5
no7 no9

supposed to be? There are either 0 or 8 diagonals here, but certainly not 2.

THE PROFESSOR SAID THAT WITH MODULUS % WE SHOULD BE ABLE TO DO IT.
Yes. If a number mod 2 results in 1, the number is odd.
Last edited on
ok this is the whole problem:

Write a C++ program that implements a game we’ll call the “hearts/clubs game”. The requirements of the game are outlined below
a. Generate 9 random numbers having possible values from 3 to 6 - including both 3 and 6. (must use srand and rand C++ functions to perform this)
b. Note: A different sequence of random numbers must be generated each time your program is run.


c. Display the character representation of the 9 random numbers in a 3x3 grid as shown below:



no1 no2 no3
no4 no5 no6
no7 no8 no9


where no1, no2, etc., corresponds to the variable names corresponding to each of the random numbers.

Typecast the number to a character as shown below for no1:

(char)no1

in order to display the character representation of that number.

Note that there are “---“ displayed between the numbers in the horizontal rows and the “|” symbol between numbers in the vertical columns.
d. Your program will compute a total score based on the values of the random numbers. The rules for computing the total score is given below:

1. If all 3 numbers in any given row, column, or diagonal (8 possibilities in all) are all odd (i.e.,any combination of only Hearts and Clubs), a score of 100 is computed for that one row, column or diagonal. Examples of these 8 possibilities are shown below:

Note: You will need to write a mathematical equation for each row, column, and diagonal (8 equations in all) that will compute the score for that row, column and diagonal. You can accomplish this without using any branching statements.



2. Compute the total score by adding up all 8 individual scores computed for each row, column diagonal in step 1.

so that is the whole homework so i did so far all the graphics and random things but now to be able to display is a row colum or diagonal is odd so 100 i do not have any clue. HELP
Well, like I said no1%2 will result in 1 if no1 is odd and 0 otherwise. And what can you do with these results?
Hint: addition, (integer) division and multiplication will help you here (in that order).
that is what i cannot understand maybe because i am so tired, but the problem is that i can say that:

int row1 = no1 + no2 + no3
so
row1%2.

that does not make sense because how i make the program to output 100 to that row and then be able to add it with the others row as the picture

http://www.mypicx.com/09202010/hearts/ and behind the pictures it says:

The total score is 300 because of all hearts/clubs in the top row, middle column, and off diagonal.
i think you gave me the perfect idea maybe if i said that no1+no2+no3 is = to row1 and the i say that row1%2 if it is like that then i can multiply by 100 and if it is no odd is going to be 0 otherwise it is going to be 100
Well, as you already noticed, calculating the modulo after adding doesn't make much sense, as the sum will be odd both for 1 and 3 odd numbers in the row. Aside from that, you're more or less on the right track.
So how about that: modulo, addition, (integer) division and multiplication (in that order).
Last edited on
ok i got it but division for what? with what number? because i now modulo is each no % 2 then each row add but then?
last question man how can i take this error out of the program it appears sometimes

c:\temp\lab2out\lab2outp2.cpp(36): warning C4244: '=' : conversion from 'time_t' to 'int', possible loss of data
Declare seed as time_t or eliminate seed entirely by merging:
srand(time(0));
Last edited on
i did this


no1 = no1%2;
no2 = no2%2;
no3 = no3%2;
no4 = no4%2;
no5 = no5%2;
no6 = no6%2;
no7 = no7%2;
no8 = no8%2;
no9 = no9%2;

row1 = (no1+no2+no3) / 2;
row2 = (no4+no5+no6) / 2;
row3 = (no7+no8+no9) / 2;
column1 = (no1+no4+no7) / 2;
column2 = (no2+no5+no8) / 2;
column3 = (no3+no6+no9) / 2;
diagonal1 = (no1+no5+no9) / 2;
diagonal2 = (no3+no5+no7) / 2;

cout << (row1 + row2 + row3 + column1 + column2 + column3 + diagonal1 + diagonal2) * 100 << endl;

but it is not working i am doing something wrong
i mean it is working but the result is wrong that why divided by 2 it can be 9/2 because no can be 3 so i do not know what to do
Why are you dividing by 2?

because no can be 3

??
so what i should do divide by 3? something that i was thinking was

no1 = no1%2;
no2 = no2%2;
no3 = no3%2;
no4 = no4%2;
no5 = no5%2;
no6 = no6%2;
no7 = no7%2;
no8 = no8%2;
no9 = no9%2;

row1 = ((no1/2)+(no2/2)+(no3/2)) / 2;
row2 = ((no4/2)+(no5/2)+(no6/2)) / 2;
row3 = ((no7/2)+(no8/2)+(no9/2)) / 2;
column1 = ((no1/2)+(no4/2)+(no7/2)) / 2;
column2 = ((no2/2)+(no5/2)+(no8/2)) / 2;
column3 = ((no3/2)+(no6/2)+(no9/2)) / 2;
diagonal1 = ((no1/2)+(no5/2)+(no9/2)) / 2;
diagonal2 = ((no3/2)+(no5/2)+(no7/2)) / 2;

that does not work either
i did it it is like this

no1 = no1%2;
no2 = no2%2;
no3 = no3%2;
no4 = no4%2;
no5 = no5%2;
no6 = no6%2;
no7 = no7%2;
no8 = no8%2;
no9 = no9%2;

row1 = (no1+no2+no3) / 3;
row2 = (no4+no5+no6) / 3;
row3 = (no7+no8+no9) / 3;
column1 = (no1+no4+no7) / 3;
column2 = (no2+no5+no8) / 3;
column3 = (no3+no6+no9) / 3;
diagonal1 = (no1+no5+no9) / 3;
diagonal2 = (no3+no5+no7) / 3;

cout << (row1 + row2 + row3 + column1 + column2 + column3 + diagonal1 + diagonal2) * 100 << endl;


thank you very much but i am still receiving the error of

1>c:\temp\lab2out\lab2outp2.cpp(36): warning C4244: '=' : conversion from 'time_t' to 'int', possible loss of data
1>c:\temp\lab2out\lab2outp2.cpp(39): warning C4244: 'argument' : conversion from 'time_t' to 'unsigned int', possible loss of data
1> lab2out.vcxproj -> C:\temp\lab2out\Debug\lab2out.exe
i have this

#include <iostream>
#include <ctime>
#include <cstdlib>

using namespace std;

int main()
{
//declaring variables
int seed = 0; //Declaration for seed
int no1= 0; //declaring random number
int no2= 0; //declaring random number
int no3= 0; //declaring random number
int no4= 0; //declaring random number
int no5= 0; //declaring random number
int no6= 0; //declaring random number
int no7= 0; //declaring random number
int no8= 0; //declaring random number
int no9= 0; //declaring random number
int row1= 0; //equation variable
int row2= 0; //equation variable
int row3= 0; //equation variable
int column1= 0; //equation variable
int column2= 0; //equation variable
int column3= 0; //equation variable
int diagonal1= 0; //equation variable
int diagonal2= 0; //equation variable



//Set the seed to be the number of seconds since 1970
seed = time(0);

//Initialize srand function with seed
srand (seed);

how can i fix that problem of the warning C4244: 'argument' : conversion from 'time_t' to 'unsigned int',
Those aren't errors. They are warnings, telling you time(0) returns a time_t and srand() only wants an integer. You can safely ignore them, or you can cast the value if you really don't want to look at the warning.

And is there a reason that row1, etc. are equal to the average value of the row/whatever? I thought for each row/col/diag of 3 odds you got 100 points. I didn't see an average come into the picture at all.

EDIT:
If you really don't want to see the warning, you can get rid of the "seed" variable entirely (it's unnecessary) and use srand(static_cast<unsigned int>(time(0))); or something.

EDIT #2:
Fixed a couple typos.
Last edited on
i really appreciate everything thank you very much
Pages: 12