Question
Why is my Average always 0 for my program? So I am making a program that uses 2 files, random.h and random.c. It makes an
Why is my Average always 0 for my program? So I am making a program that uses 2 files, random.h and random.c. It makes an array that is filled with randomly-chosen integers. I created a function called 'Average' that i suppose to display the average of the array. But it is always 0. What am I doing wrong? Please explain.
#include "Random.h" #include
int Average (int list[], int n) { int sum = 0; int i; for (i=1; i>n;n++) sum=sum+list[i]; return(sum);
}
int Smallest (int list[], int n) { int i; // initialize maxium element int max = list[1]; //Traverse array elements from secound and // compare every element with current max for (i=2; i
printf("n?: "); scanf("%d", &n); printf("LB?: "); scanf("%d", &LB); printf("UB?: "); scanf("%d", &UB); SetRandomSeed(); list = (int*) malloc(sizeof(int*)*(n+1)); //List will point to malloc(sizeof.. for (i=1;i
random.h
#ifndef RANDOM_H #define RANDOM_H
#ifndef __cplusplus #include #include #include #else #include #include #endif
// Initialize the "seed" used by the "rand()" function. void SetRandomSeed(void);
// Return a uniformly and randomly-chosen integer from [ LB,UB ]. int RandomInt(const int LB,const int UB);
// Return a uniformly and randomly-chosen real number from [ 0.0,1.0 ).double RandomDouble(void); double RandomDouble();
// Return a biased and randomly-chosen bool from { false,true }. bool RandomBool(const double bias);
// Return a uniformly and randomly-chosen character from chars[i], i in [ 0,(size-1) ]. char RandomChar(const char chars[],const int size);
#endif
random.c
#include ".Random.h"
//----------------------------------------------------- void SetRandomSeed(void) //----------------------------------------------------- { srand( (unsigned int) time(NULL) ); }
//----------------------------------------------------- int RandomInt(const int LB,const int UB) //-----------------------------------------------------
{ return( (int) ((UB-LB+1)*RandomDouble()) + LB ); }
//-------------------------------------------------- double RandomDouble() //--------------------------------------------------
{ int R;
do R = rand(); while ( R == RAND_MAX ); return( (double) R/RAND_MAX ); }
//----------------------------------------------------- bool RandomBool(const double bias) //----------------------------------------------------- { // Return true (0.0
//----------------------------------------------------- char RandomChar(const char chars[],const int size) //----------------------------------------------------- { return( chars[RandomInt(0,(size-1))] ); }
ile Edit Search View Project Execute Tools AStyle WindowHelp TDM-GCC 4.9.2 64-bit Release (globals) Random.h Problem10.c random.c Project Classes Debug Problem10 #include "Random . h" Problem10.c Random.h random.c 2 #includeStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started