Generate random numbers in array and count the average, max, min, sum - c

This code is supposed to generate 30 random numbers, 0-100, and print the average, max value, and the min value. But it has logical errors and I cant help but think I've made a stupid mistake.
****code is supposed to generate 30 numbers from 0-100, and display mean, max, and min****
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define SIZE 30
int generateRandom(void);
int main(void)
{
int points[SIZE], i, sum, max, min, num;
double average;
srand(time(NULL)); /*Seed random number generator*/
num = generateRandom(); /*Genrate the random numbers*/
printf("num = %d\n", num); /*Print the random numbers*/
sum = 0;
for ( i = 0; i < SIZE; i++) /*Find the average*/
{
sum += points[i];
average = sum / SIZE;
}
printf("Average = %f", average); /*Print the average*/
max = points[0]; /*initialize the max to 0*/
for ( i = 0; i < SIZE; i++) /*find the min*/
{
if (points[i] > max)
{
max = points[i];
}
}
printf("Maximum = %d\n", max); /*print the maximum number*/
min = points[0]; /*initialize the min*/
for ( i = 0; i < SIZE; i++) /*Find the min*/
{
if (points[i] < min)
{
min = points[i];
}
}
printf("Minimum = %d\n", min); /*Print the minimum number*/
return 0;
}
int generateRandom(void)
{
int random;
random = rand() % 101;
return random;
}

You generate just one random number, which you store in num. Then your code behaves as if it had 30 random numbers stored in points. But it never makes this happen.

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define SIZE 30
int generateRandom(void);
int main(void)
{
int points[SIZE],i,sum,max,min,num;
double average;
srand(time(NULL));
for(i = 0; i < SIZE; i++){
points[i] = generateRandom();
}
sum = 0;
for(i = 0; i < SIZE; i++)
{
sum += points[i];
}
average = (sum * 1.0) / SIZE; /*you need double here*/
printf("Average = %f\n",average);
max = points[0];
for(i = 1; i < SIZE; i++)
{
if(points[i] > max)
{
max = points[i];
}
}
printf("Maximum = %d\n",max);
min = points[0];
for(i = 1; i < SIZE; i++)
{
if(points[i] < min)
{
min = points[i];
}
}
printf("Minimum = %d\n",min);
return 0;
}
int generateRandom(void)
{
int random;
random = rand() % 101;
return random;
}

Related

how can i replace scanf with for loop counter?

I am writing a program in order to count all achilles numbers without the math.h library. In this programm first I calculate powerful numbers after that I calculate GCD of powerful number. If GCD is 1 then the current number is achilles.
My problem is that my program works with scanf() but not with for loop counter! What i am doing wrong?
Thank you very much!
#include <stdio.h>
#define MAX 1000
int main(void)
{
int n;
int i, j, a;
int counter2 = 0;
int large;
int small;
int rem, gcd, max = 1, min = 1;
int achilles;
for (a = 1; a <= MAX; a++) //for loop for counter
{
n=a;
for (i = 1; i <= n; i++)
{
int count = 0;
for (j = 1; j <= i; j++)
{
if (i % j == 0)
{
count++;
}
}
int l = 0;
if (count == 2)
{
while (n % i == 0) // calculate factor and his exponent
{
l++;
n = n / i;
}
if (l > max) // calculates min and max in order to find GCD
{
max = l;
}
if (l < min)
{
min = l;
}
}
large = max;
small = min;
while (small) { // While small is not 0
// Calculates GCD
rem = large % small;
large = small;
small = rem;
}
gcd = large;
// printf("GCD(%d,%d)= %d", large, small, gcd);
}
if (gcd == 1) {
achilles = n;
// printf("%d\n", achilles);
}
// printf("GCD(%d,%d)= %d", max, min, gcd);
printf("%d\n", achilles);
}
}
The programm before editing with the for loop is the following!
#include <stdio.h>
#define MAX 1000
int main(void)
{
int n;
int i, j, a;
int counter2 = 0;
int large;
int small;
int rem, gcd, max = 1, min = 1;
int achilles;
scanf("%d", &n);
printf("%d = ",n);
for (i = 1; i <= n; i++)
{
int count = 0;
for (j = 1; j <= i; j++)
{
if (i % j == 0)
{
count++;
}
}
int l = 0;
if (count == 2)
{
while (n % i == 0) // calculate factor and his exponent
{
l++;
n = n / i;
}
if (l > max) // calculates min and max in order to find GCD
{
max = l;
}
if (l < min)
{
min = l;
}
}
large = max;
small = min;
while (small) { // While small is not 0
// Calculates GCD
rem = large % small;
large = small;
small = rem;
}
gcd = large;
// printf("GCD(%d,%d)= %d", large, small, gcd);
}
/*if (gcd == 1) {
achilles = n;
// printf("%d\n", achilles);
}*/
printf("GCD(%d,%d)= %d", max, min, gcd);
//printf("%d\n", achilles);
}

Statistics of an array

I need a program to evaluate Min, Max, Avg and Geometric avg of any number of integers. This is what I've come up with so far. Min and Max were working just fine until I added the Avg. Now Min and Avg is working right but Max gives wrong number (usually the second greatest number). Also geometric avg gives only 0.00000. Thanks for your help.
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main(int argc, char const *argv[]) {
int arr[100], max, i, min, size, lok = 1;
float arit = 0, geom = 0, sum = 0, prod = 0;
printf("\nSay how many integers you want to input: ");
scanf("%d", &size);
printf("\nType %d integers: ", size);
for (i = 0; i < size; i++) //put values in arr
scanf("%d", &arr[i]);
max = arr[0];
min = arr[0];
for (i = 1; i < size; i++) { //calc maximum
if (arr[i]>max) {
max = arr[i];
lok = i+1;
}
if (arr[i]<min) { //calc minimum
min = arr[i];
lok = i+1;
}
for (i = 0; i < size; i++) { //calc avg
sum = sum + arr[i];
}
arit = sum/size;
for (i = 0; i < size; i++) {
prod = prod * arr[i];
}
geom = pow(prod, 1./size);
}
printf("\n%d is maximum", max);
printf("\n%d is minimum", min);
printf("\n%f is avg", arit);
printf("\n%f is geometric avg", geom);
return 0;
}
The two main problems were
a misplaced closing } brace
wrong intialisation of prod = 0
I have made some other changes too:
check the validity of the inputs
use double instead of float (unless a good reason not to).
remove the unused lok
only one loop is needed for all the statistics
moved the position of the \n newlines as is customary.
This is the amended code:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main(int argc, char const *argv[]) {
int arr[100], max, i, min, size;
double arit = 0, geom = 0, sum = 0, prod = 1; // init prod to 1
printf("\nSay how many integers you want to input: ");
if(scanf("%d", &size) != 1 || size < 1 || size > 100) {
exit(1); // or other action
}
printf("\nType %d integers:\n", size); // added newline
for (i = 0; i < size; i++) {
if(scanf("%d", &arr[i]) != 1) {
exit(1); // or other action
}
}
max = arr[0];
min = arr[0];
for (i = 0; i < size; i++) { // just one loop
if (arr[i] > max) {
max = arr[i];
}
if (arr[i] < min) {
min = arr[i];
}
sum = sum + arr[i];
prod = prod * arr[i];
}
arit = sum / size;
geom = pow(prod, 1.0 / size);
printf("%d is maximum\n", max); // reposition newlines
printf("%d is minimum\n", min);
printf("%f is avg\n", arit);
printf("%f is geometric avg\n", geom);
return 0;
}
This will fix your problem with Min and Max, and I guess also will fix geometric mean(check it) It was a problem closing for sentences
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main(int argc, char const *argv[]) {
int arr[100], max, i, min, size, lok = 1;
float arit = 0, geom = 0, sum = 0, prod = 1;
printf("\nSay how many integers you want to input: ");
scanf("%d", &size);
printf("\nType %d integers: ", size);
for (i = 0; i < size; i++) //put values in arr
scanf("%d", &arr[i]);
max = arr[0];
min = arr[0];
for (i = 1; i < size; i++) { //calc maximum
if (arr[i]>max) {
max = arr[i];
lok = i+1;
}
if (arr[i]<min) { //calc minimum
min = arr[i];
lok = i+1;
}
}
for (i = 0; i < size; i++) { //calc avg
sum = sum + arr[i];
}
arit = sum/size;
for (i = 0; i < size; i++) {
prod = prod * arr[i];
}
geom = pow(prod, 1./size);
printf("\n%d is maximum", max);
printf("\n%d is minimum", min);
printf("\n%f is avg", arit);
printf("\n%f is geometric avg", geom);
return 0;
}
About Geometric mean you are doing this.
float arit = 0, geom = 0, sum = 0, prod = 0;
and then
for (i = 0; i < size; i++) {
prod = prod * arr[i];
}
That allways will return 0 because you initialize prod at 0

Find min,max,average values throw array

I made this code but I can't stop it. This code abou finding min, max, average values. What should I do?
#include <stdio.h>
void get_stat_per_test(int score[][3])
{
int i, k, min, max, sum;
for (k = 0; k<3; k++){
min = max = score[0][k];
sum = 0;
for (i = 0; i<10; i++){
if (score[i][k]<min) min = score[i][k];
if (score[i][k]>min) max = score[i][k];
sum += score[i][k];
}
printf("Max=%d\n", max);
printf("MIn=%d\n", min);
printf("Average=%f\n", sum / (3.0 * 10));
}
}
int main(void)
{
int i, k;
int score[10][3];
for (i = 0; i<10; i++){
for (k = 0; k<3; k++){
printf("The result is %d student in %d exam: ", i, k);
scanf("%d", &score[i][k]);
}
}
get_stat_per_test(score);
return 0;
}
After student 5 should stop.

Finding Max value and Average Of All elemements in 2D array in C

EDIT II
I'm finally getting somewhere, now i get my random values with a return from the maxavg() function.
My only question now is, when i run the program i always get:
average: 0
maximum: 33
why? it does not make much sense.
Here is the new code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <limits.h>
int GetRand(int min, int max);
struct maxavg;
int main ()
{
int a[21][21], i , j, average, maximum, ret;
for (i = 0; i < 21; i++)
{
for ( j = 0; j < 21; j++)
{
a[i][j] = GetRand(0, 100);
printf("%3d" , a[i][j]);
}
a[2][15] = -1;
a[10][6] = -1;
a[13][5] = -1;
a[15][17] = -1;
a[17][17] = -1;
a[19][6] = -1;
printf("\n");
}
printf("average = %d \n maximum = %d", average, maximum);
return 0;
}
// random seed
int GetRand(int min, int max);
int get ()
{
int i, r;
for (i = 0; i < 21; i++)
{
r = GetRand(0, 100);
printf("Your number is %d \n", r);
}
return(0);
}
int GetRand(int min, int max)
{
static int Init = 0;
int rc;
if (Init == 0)
{
srand(time(NULL));
Init = 1;
}
rc = (rand() % (max - min +1) +min);
return (rc);
}
struct pair
{
int max;
int avg;
};
// max and average
struct pair maxavg()
{
struct pair p;
int max=INT_MIN, sum=0, count=0, avg, i, j, current;
for(i = 0; i < 21; i++){
for(j =0; j < 21; j++){
if(current > -1){
sum = sum + current;
count = count + 1
;if(current > max){
max = current;
}
}
}
}
avg = sum/count;
printf("Max is %d \n", max);
printf("Average is %d \n", avg);
p.max = max;
p.avg = avg;
return p;
}
EDIT:
So here is what i'm doing, i get error messages:
Average:
// Average Code
value = a[i][j];
int actualvalue, suma = 0, quant;
for(i=0; i<21; i++){
for(j=0; j<21; j++){
if (actualvalue > -1){
a[i][j] = actualvalue;
suma = suma + actualvalue;
// sum actual value + nextvalue (sum of all > -1) //
}
else if {
quant = quant + 1;
//(sum the quantity of times a value has been greater than -1)//
}
}
}
printf("The average value is:", suma/quant); ///(sun of all values > -1)/(sum of quantity value was > -1)/
Find Maximum:
// Max
int variableP = a[0][0];
value = a[i][j];
int variableP = a[i][j]
for(i=0; i<21; i++){
for(j=0; j<21; j++){
if(variableP < newvalue){
variableP = newvalue
}
}
}
printf("The max value of the 2D array is", %d);
Average and Maximum:
// max and average
int maxvg();
int max=INT_MIN, sum=0, count=0, avg;
for(i = 0; i < 21; i++){
for(j =0; j < 21; j++){
if(current > -1){
sum = sum + current;
count = count + 1
if(current > max){
max = current;
}
}
}
}
avg = sum/count;
printf("Max is %d \n", max);
printf("Average is %d \n", avg);
So how right or wrong is this? what am i missing.
i mostly get:
[Error] expected '=', ',', ';', 'asm' or '__attribute__' before '<' token
[Warning] data definition has no type or storage class
[Error] initializer element is not constant
[Error] expected declaration specifiers or '...' before string constant
Am i at least close to it?
Thanks in advance.
END OF EDIT
I created a 2D array with random numbers from 0 to 100 (and a couple of -1) values with this code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main ()
{
int a[21][21], i, j;
for (i = 0; i < 21; i++) {
for (j = 0; j < 21; j++) {
a[i][j] = GetRand (0, 100);
a[7][15] = -1;
a[10][6] = -1;
a[13][5] = -1;
a[15][17] = -1;
a[17][17] = -1;
a[19][6] = -1;
printf ("%3d", a[i][j]);
}
printf ("\n");
}
return 0;
}
// random seed
int GetRand (int min, int max);
int get ()
{
int i, r;
for (i = 0; i < 21; i++) {
r = GetRand (0, 100);
printf ("Your number is %d \n", r);
}
return (0);
}
int GetRand (int min, int max)
{
static int Init = 0;
int rc;
if (Init == 0) {
srand (time (NULL));
Init = 1;
}
rc = (rand () % (max - min + 1) + min);
return (rc);
}
This prints the array created. Now I want to calculate the maximum value of all values inside the array and the total average of all values in the array, all while ignoring all -1 values, so only from 0 to 100. Since I'm a total beginner I'm having problems creating these functions. So here are my ideas.
//For the average
for(i=0; i<1; i++){
for(j=0; j<21; j++){
if (actualvalue > -1){
//sum actualvalue + nextvalue (sum of all the values greater than -1)//
}
else if (actualvalue > -1){
//(sum the quantity of times a value has been greater than -1)//
}
}
}
}
printf("The average value is", //(sum of all values>-1)/(sum of quantity value was >-1) //);
I'm representing the thing i don't know how to write in code in words so you get my idea.
Now for finding the maximum: what i think i should do is initialize the array and make a variable adopt the first value it finds that's > -1, then rewind and initialize again, if the actualvalue < newvalue then make variableP adopt the newvalue:
//max
int variableP = a[i][j]
for(i=0; i<21; i++){
for(j=0;j<21;j++){
if(variableP < newvalue){
variableP = newvalue
}
printf("The max value of the 2D array is", %d);
}
I know it's evident i'm not sure what i'm writing here, but i think my idea of it is correct, i hope i'm explaining it well enough.
Sum is as you say.
Average requires you count number of >-1 values.
Max looks right lines. Finish off your ideas and ask again if it doesn't work
You can do both max and avg in one loop.
Declare three variables: max=-999999, sum=0, count=0.
Each time when current cell is not -1 increase sum by it's value and count by 1.
Each time check if current value is bigger than max, then set max to current value.
After the loop is done, avg = sum/count.
Consider you are doing all your arithmetic using integers, and integer division is integer. Perhaps you have better to do a double result with:
/* you don't actually need to cast to double in both operators, as the
* one not casted will be automagically casted to double to operate on.
*/
double average = (double) sum_of_samples / (double) number_of_samples;
You have to cast at least one of the operators, because if you don't you'll get the result as before (a 0 integer result) converted to double to assign to the variable.

Max and Min value in array

My program is giving me two minimum values and no maximum values. My program is supposed to print max value if opcode is 1 and min value if opcode is 0. Help please
#include <stdio.h>
int minmax(int array[], int array_size, int opcode);
int main(void)
{
int array[]= {99,4,95,2,98}; //array size
int array_size = 5;
int i;
int opcode;
array_size = 5;
for (i = 0; i < array_size; i++)
{
printf("array[%d] = %d\n", i , array[i]);
}
printf("Enter an opcode 0 or 1: ");
scanf("%d", &opcode);
minmax(array, array_size, opcode);
}
int minmax(int array[], int array_size, int opcode)
{
int i;
int max = array[0];
int min = array[0];
for (i = 0; i < array_size; i++)
{
if (opcode == 1 && array[i] > max)
{
max = array[i];
printf("The max value is: %d\n", max);
}
else if (opcode == 0 && array[i] < min)
{
min = array[i];
printf("The min value is: %d\n", min);
}
}
return 0;
}
It does not print the maximum because the first element itself is the maximum.
That is,
if (opcode == 1 && array[i] > max)
{
max = array[i];
printf("The max value is: %d\n", max);
}
never executes in this code.
The remedy to be output the minimum or maximum at the end of the minmax() function.
Your code should look like:
int minmax(int array[], int array_size, int opcode)
{
int i;
int max = array[0];
int min = array[0];
for (i = 0; i < array_size; i++)
{
if (opcode == 1 && array[i] > max)
{
max = array[i];
printf("The max value is: %d\n", max);
}
else if (opcode == 0 && array[i] < min)
{
min = array[i];
printf("The min value is: %d\n", min);
}
}
//The below code prints the final max / min (as determined by opcode)
if (opcode == 1) printf("The final maximum is %d\n",max);
else printf("The final minimum is %d\n",min);
return 0;
}

Resources