Smart questions
Smart answers
Smart people
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Member Login

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips now!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

Join Tek-Tips
*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

LINK TO THIS FORUM!

Add Stickiness To Your Site By Linking To This Professionally Managed Technical Forum.
Just copy and paste the
code below into your site.

Partner With Us!

"Best Of Breed" Forums Add Stickiness To Your Site
Partner Button
(Download This Button Today!)

Feedback

"...Just a quick note to say, "THANKS!" for these forums...The site is very well layed out and easy to use. Thanks for bringing us together - we need each other."

Geography

Where in the world do Tek-Tips members come from?

arrays...cant find the error in my programHelpful Member! 

watshamacalit (Programmer)
10 Dec 02 19:57
Write a program that rolls a six-sided die 6000 times and displays the
number of times each face was rolled.

so far i have...

#include <stdio.h>

void main ( void )
{
int index, count, roll ;
int face[ 6 ] ;

for ( index = 1 ; index <= 5 ; index++ )
face[ index ] = 0 ;

printf( "Face\tFrequency\n" ) ;
printf( "----\t---------\n" ) ;

for ( roll = 0 ; roll <= 10 ; roll++ )
++face[ roll ] ;

for ( count = 1 ; count <= 6 ; count++ )
printf( "%4i\t%9i\n", count, face[ index ] ) ;

}

this doesnt work... the compiler says it has created a fatal error... whats wrong with the program?
marsd (IS/IT--Management)
10 Dec 02 20:42
Try this.

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

#define MAX 6000

int *intArray();
int dieRoll(void);
void frequencyCheck(int *,int);


int main(void) {
int *array = NULL;
int y = 0;

      if ( (array = intArray()) != NULL) {
          while (y++ < MAX) {
                 array[y] = dieRoll();
          }
          
          for (y=1 ; y <= 6 ; y++) {
              frequencyCheck(array,y);
          }
          free(array);
          return 0;
       }
return 1;
}           
 
int *intArray() {
int *arr = malloc(MAX * sizeof(int));    
    
       if (arr) {
           return arr;
          }
return NULL;
}
        
int dieRoll() {
return (1 + rand() % 6);
}

void frequencyCheck(int *arr, int n) {
int y,x = 0;
      for (y=0 ; y <= MAX ; y++) {
           if (arr[y] == n) {
              x++;
           }
       }
    printf("%d occurred %d times\n", n, x);
}  
Helpful Member!  nkm (Programmer)
10 Dec 02 23:31
Hi

There are a few errors in your pgm

1) You have defined the array int face[ 6 ]

But you start intialising it from index 1 to 5.
The indexing to arrays start from 0 to size -1 so in your case 0 - 5.
This could case a runtime error as your face[0] is not initialised.

2) In the second loop you are writing beyond the array boundary when you index 6,7,... in it. You should keep the upper limit as 5 only.

3) Third loop again you have indexed 6 which is beyond your array bounds.

4) The program that you have written does not achive what your objective was it would merely increment each array location to 1 and print it.
Follow the marsd example.




 

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Back To Forum

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close