Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

The counter which tracks how many times a number is used at the bottom is not wo

ID: 3640402 • Letter: T

Question

The counter which tracks how many times a number is used at the bottom is not working

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

#define RANGE 10

int main()
{
int r[1000];
int num;
int count;
float sum;
float sum2;
int numgened[10];
int count2;

sum = 0;
srand((unsigned)time(NULL));

for (count = 0; count < 1000; count++)
{
r[count] = 0;
}

for (count2 = 0; count2 < 10; count2++)
{
numgened[ count2 ]=0;
}

for (count = 0; count < 1000; count++)
{
num = rand() % RANGE + 1;
r[count] = num;

if ( num = 1)
{
numgened [ 0 ]++;
}
if ( num = 2)
{
numgened [ 1 ]++;
}
if ( num = 3)
{
numgened [ 2 ]++;
}
if ( num = 4)
{
numgened [ 3 ]++;
}
if ( num = 5)
{
numgened [ 4 ]++;
}
if ( num = 6)
{
numgened [ 5 ]++;
}
if ( num = 7)
{
numgened [ 6 ]++;
}
if ( num = 8)
{
numgened [ 7 ]++;
}
if ( num = 9)
{
numgened [ 8 ]++;
}
if ( num = 10)
{
numgened [ 9 ]++;
}
}

for (count = 0; count < 1000; count++)
{
sum += r[count];
}

printf("1: %i ", numgened[0]);
printf("2: %i ", numgened[1]);
printf("3: %i ", numgened[2]);
printf("4: %i ", numgened[3]);
printf("5: %i ", numgened[4]);
printf("6: %i ", numgened[5]);
printf("7: %i ", numgened[6]);
printf("8: %i ", numgened[7]);
printf("9: %i ", numgened[8]);
printf("10: %i ", numgened[9]);

printf("The sum is: %f ", sum);

sum2=sum/1000;

printf("The average is: %f ", sum2);



return(0);
}

Explanation / Answer

if (num == 1) not if (num = 1)...

one is equality operator and the other is assignment operator. This is a very common mistake.

Optional: use if else statement instead of 10 if statements

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

#define RANGE 10

int main()
{
   int r[1000];
   int num;
   int count;
   float sum;
   float sum2;
   int numgened[10];
   int count2;

   sum = 0;
   srand((unsigned)time(NULL));

   for (count = 0; count < 1000; count++)
   {
      r[count] = 0;
   }

   for (count2 = 0; count2 < 10; count2++)
   {
      numgened[count2] = 0;
   }

   for (count = 0; count < 1000; count++)
   {
      num = rand() % RANGE + 1;
      r[count] = num;

      if (num == 1)
      {
         numgened[0]++;
      }
      else if (num == 2)
      {
         numgened[1]++;
      }
      else if (num == 3)
      {
         numgened[2]++;
      }
      else if (num == 4)
      {
         numgened[3]++;
      }
      else if (num == 5)
      {
         numgened[4]++;
      }
      else if (num == 6)
      {
         numgened[5]++;
      }
      else if (num == 7)
      {
         numgened[6]++;
      }
      else if (num == 8)
      {
         numgened[7]++;
      }
      else if (num == 9)
      {
         numgened[8]++;
      }
      else if (num == 10)
      {
         numgened[9]++;
      }
   }

   for (count = 0; count < 1000; count++)
   {
      sum += r[count];
   }

   printf("1: %i ", numgened[0]);
   printf("2: %i ", numgened[1]);
   printf("3: %i ", numgened[2]);
   printf("4: %i ", numgened[3]);
   printf("5: %i ", numgened[4]);
   printf("6: %i ", numgened[5]);
   printf("7: %i ", numgened[6]);
   printf("8: %i ", numgened[7]);
   printf("9: %i ", numgened[8]);
   printf("10: %i ", numgened[9]);

   printf("The sum is: %f ", sum);

   sum2 = sum/1000;

   printf("The average is: %f ", sum2);

   return(0);
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote