Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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.

image text in transcribed

#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 #include 3 4 5 int Average (int list[], int n) 6 7 int sum0 8 int i; 9 for (i-1; i>n;n++) 10 sum sum+list[i]; 12 return(sum); 13 14 L ) 15 16 17 int Smallest (int list[], int n) 19 int i; 21 22 // initialize maxium element 23 | int max list [1]; 24 25 /Traverse array elements from secound and 26|// compare every element with current max

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Excel As Your Database

Authors: Paul Cornell

1st Edition

1590597516, 978-1590597514

More Books

Students also viewed these Databases questions