OMG More C Help!

Locked
fr0d
Posts: 584
Joined: Fri May 17, 2002 11:48 pm

Post by fr0d »


I have been racking my brain for awhile on this stupid problem. I don't even know where to start since the douche bag instructor was sick and said 'you can learn arrays all by yourselves' I was like WTF no I cant!


 


Anyways here is the question;


 


Write a sprogram that simulates the rolling of two dice. The program should use rand to roll the first die, and shoul use rand again to roll the second die. The sum of the two values should then be calculated. [Note: Since each die can show an integer value from 1 to 6, then the sum of the two value will vary from 2 to 12 with 7 being the most frequent sum and 2 and 12 beiing the least frequent sums] Figure 6.23 shows the 36 possible combinations of the two dice. You program should roll the two dice 36,000 times. Use a single-subscripted array to tally the numbers of times each possible sum appears. Print the results in a tabular format. Also, determine if the totals are reasonable. I.e. there are six ways to roll a 7, so approximately one sixth of all the rolls will be 7.


 


 


 


thanks in advance! LAL


MikeJ
Posts: 686
Joined: Fri Oct 26, 2001 9:11 am

Post by MikeJ »



/*
 Sample Output:
 
 Roll Totals From Sum of Dice:
 
   2: 1031
   3: 2055
   4: 3018
   5: 3902
   6: 5072
   7: 5955
   8: 4977
   9: 3977
   10: 3038
   11: 1996
   12: 979
*/

#include <stdio.h>
#include <time.h>
#include <stdlib.h>

int rollDie( void );

int main()
{
 
 int i;
 int die1, die2;
 int total;
 int resultArray[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
 
 srand( time(NULL) );  /*set the seed using the current time */
 
 for ( i = 0; i < 36000; i++ )
 {
   die1 = rollDie();
   die2 = rollDie();
   
   total = die1 + die2;
   
   resultArray[total - 1]++;
 }
 
 printf( "Roll Totals From Sum of Dice: \n\n" );
 
 for ( i = 1; i < 12; i++ )
 {
   /* skip 1 because sum is minimum 2 */
   printf( "  %i: %i\n", i+1, resultArray[i] );
 }
 
 printf( "\n" );

 return 0;
}

int rollDie()
{
 /* rolls a die using rand and returns the value */
 
 int val;

 while ( 6 <= ( val = rand() / (RAND_MAX / 6 ) ) );
 
 switch ( val )
 {
   case 0:
     return 1;
   case 1:
     return 2;
   case 2:
     return 3;
   case 3:
     return 4;
   case 4:
     return 5;    
   case 5:
     return 6;
 }
}



 


I don't know if that is what you would call a tabular output, let me know if it should look different. Also, let me know if the output has to show which combinations were used to add up to final total. The instructions seem kinda vague tbh.


dj_de
Posts: 604
Joined: Tue Nov 27, 2001 11:45 pm

Post by dj_de »

mikej: i think using rand lets you define what range of numbers to return, and then you can just add it. so you shouldnt need something to skip the sum of 1

fr0d
Posts: 584
Joined: Fri May 17, 2002 11:48 pm

Post by fr0d »



/*
?Sample Output:
?
?Roll Totals From Sum of Dice:
?
? ?2: 1031
? ?3: 2055
? ?4: 3018
? ?5: 3902
? ?6: 5072
? ?7: 5955
? ?8: 4977
? ?9: 3977
? ?10: 3038
? ?11: 1996
? ?12: 979
*/

#include <stdio.h>
#include <time.h>
#include <stdlib.h>

int rollDie( void );

int main()
{
?
?int i;
?int die1, die2;
?int total;
?int resultArray[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
?
?srand( time(NULL) ); ?/*set the seed using the current time */
?
?for ( i = 0; i < 36000; i++ )
?{
? ?die1 = rollDie();
? ?die2 = rollDie();
? ?
? ?total = die1 + die2;
? ?
? ?resultArray[total - 1]++;
?}
?
?printf( "Roll Totals From Sum of Dice: \n\n" );
?
?for ( i = 1; i < 12; i++ )
?{
? ?/* skip 1 because sum is minimum 2 */
? ?printf( " ?%i: %i\n", i+1, resultArray[i] );
?}
?
?printf( "\n" );

?return 0;
}

int rollDie()
{
?/* rolls a die using rand and returns the value */
?
?int val;

?while ( 6 <= ( val = rand() / (RAND_MAX / 6 ) ) );
?
?switch ( val )
?{
? ?case 0:
? ? ?return 1;
? ?case 1:
? ? ?return 2;
? ?case 2:
? ? ?return 3;
? ?case 3:
? ? ?return 4;
? ?case 4:
? ? ?return 5; ? ?
? ?case 5:
? ? ?return 6;
?}
}



 


I don't know if that is what you would call a tabular output, let me know if it should look different.  Also, let me know if the output has to show which combinations were used to add up to final total.  The instructions seem kinda vague tbh.







 


Yea the question is pretty vauge. I think that is the output that was wanted. I am having such a hard time with this course because he only reads the PDF's that came with the text book during lectures, and most of the people in that class are going into programming.


 


Thanks again!


 


I also have one more question, how would I go about multiplying two four by four matrices.


The prototype should given to me was, but i have no clue what to do with it.


void matMult(int a[][4],int b[][4],int c[][4]);


MikeJ
Posts: 686
Joined: Fri Oct 26, 2001 9:11 am

Post by MikeJ »


Ok i went ahead and attempted the matrix thing, let me know if this actually prints the correct answer :o



#include <stdio.h>

void matMult( int a[][4], int b[][4], int c[][4] );

int main()
{

 static int a[4][4] = {
   {1, 4, 7, 9 },
   {4, 5, 2, 7 },
   {9, 8, 5, 4 },
   {3, 6, 1, 6 },
 };

 static int b[4][4] = {
   {5, 3, 9, 1 },
   {3, 7, 5, 6 },
   {3, 5, 1, 4 },
   {8, 3, 2, 4 },
 };

 int c[4][4] = {
   {0, 0, 0, 0 },
   {0, 0, 0, 0 },
   {0, 0, 0, 0 },
   {0, 0, 0, 0 },
 };

 matMult( a, b, c );

 return 0;

}

void matMult( int a[][4], int b[][4], int c[][4] )
{
 int i, j, k;

 for ( i = 0; i < 4; i++ )
 {
   for ( j = 0; j < 4; j++ )
   {
     for ( k = 0; k < 4; k++ )
     {
       c[j][i] += ( a[j][k] * b[k][i] );
     }
   }
 }

 printf("\n");

 for ( i = 0; i < 4; i++ )
 {
   for ( j = 0; j < 4; j++ )
   {
     printf("%i ", c[i][j]);
   }
   printf("\n");
 }
 printf("\n");

}

 



matrices "a" and "b" in main() are declared just as they would be on paper in a math class, so you can add in your own values.


Michael-Corleone
Posts: 1398
Joined: Wed Apr 03, 2002 8:11 am

Post by Michael-Corleone »


Ok i went ahead and attempted the matrix thing, let me know if this actually prints the correct answer :o


#include <stdio.h>

void matMult( int a[][4], int b[][4], int c[][4] );

int main()
{

 static int a[4][4] = {
   {1, 4, 7, 9 },
   {4, 5, 2, 7 },
   {9, 8, 5, 4 },
   {3, 6, 1, 6 },
 };

 static int b[4][4] = {
   {5, 3, 9, 1 },
   {3, 7, 5, 6 },
   {3, 5, 1, 4 },
   {8, 3, 2, 4 },
 };

 int c[4][4] = {
   {0, 0, 0, 0 },
   {0, 0, 0, 0 },
   {0, 0, 0, 0 },
   {0, 0, 0, 0 },
 };

 matMult( a, b, c );

 return 0;

}

void matMult( int a[][4], int b[][4], int c[][4] )
{
 int i, j, k;

 for ( i = 0; i < 4; i++ )
 {
   for ( j = 0; j < 4; j++ )
   {
     for ( k = 0; k < 4; k++ )
     {
       c[j][i] += ( a[j][k] * b[k][i] );
     }
   }
 }

 printf("\n");

 for ( i = 0; i < 4; i++ )
 {
   for ( j = 0; j < 4; j++ )
   {
     printf("%i ", c[i][j]);
   }
   printf("\n");
 }
 printf("\n");

}


 



matrices "a" and "b" in main() are declared just as they would be on paper in a math class, so you can add in your own values.







yeah that should work :tree:


fr0d
Posts: 584
Joined: Fri May 17, 2002 11:48 pm

Post by fr0d »


Ok, so basically I've been studying way too much for other classes (when you have 8 per semester, C++ doesn't get much time) And I have NO clue what this question is asking.


 


Write a C function called input which returns void,

this function prompts the user for input of two integers followed by


a double precision value.  This function reads these values from the keyboard


and finds the product of the two integers entered.  The function uses


call by reference to communicate the values of the three values read


and the product calculated back to the main program.  The main program then


prints the three values read and the product calculated.


Provide test results for the input: 3 5 23.5.



 


Now, I know I have to do something like this



 #include <stdio.h>
void productByReference( int *nPtr );

int main()
{
program
}
void productByReference( int *nPtr )
{
*nPtr = READ IN THE FUCKING INTEGERS AND SUM THEM SOME HOW BECUASE THE BOOK ONLY GIVES ME EXAMPLES WITH ONE INTEGER AND I HAVE TWO AND I DONT KNOW WHAT TO FUCKING DO I HATE C++ BOOKS!
}



 


So, what do I have to do?


fr0d
Posts: 584
Joined: Fri May 17, 2002 11:48 pm

Post by fr0d »


For Fucks Sake! I'm just gonna put in the rest of the questions. I look at the code in the text book and it looks like fucking russian too me. Anyways here are the second two questions.


 


Write a program that inputs four strings that represent integers, converts the strings to integers, sums the values and prints the total of the four values.

Now for this question, I know I have to use the command, i = atoi( "integer value" ) but I don't know how to get my values into that formula.


 


If I use the gets( value ); command how do I turn that into an integer I can use with the atoi command? There are no examples like this shit in the text book and I hate it!


 


Write a C program which reads 25 integer numbers from a file

named dat.in, finds the average of these numbers and writes this average


in the file dat.out. Use fopen function calls to accomplish this


(do not use the DOS or Unix redirect < or > features)


provide a printed copy of your program code and the files dat.in


and dat.out.



 


Great, because I have a conflict with your class, and you don't give me help when I ask, what the fuck am I supposed to do here. ARRGGHHH GOD I HATE COMPUTERS AND I NEVER WANT TO WORK WITH THEM AGAIN!


 


I envy anyone who can do this sorta stuff.


MikeJ
Posts: 686
Joined: Fri Oct 26, 2001 9:11 am

Post by MikeJ »

I can code all of that if i have time how much time do you have ??

fr0d
Posts: 584
Joined: Fri May 17, 2002 11:48 pm

Post by fr0d »


I can code all of that if i have time how much time do you have ??






I don't really need the code, because I told him I'm not handing in my assignment because I hate his class (not really).


 


But I just want some hints on how exactly do I read a string from the keyboard, what is call by reference and inputs a void or whatever it means.


 


I'm pretty C stupid :P


Locked