-4

So this is a lab where we're supposed to create a random number generator to generate at least 100 integers in an int array. I believe my code may have a few errors in it, but i'm getting 57 errors on my compiler and I just don't see that many. What's wrong with the code below?

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

int min, max, lowest = 0, highest = 0;
float average;
int randoms [100];

int main()
{       
    printf("Enter a minimun value.\n");
    scanf("%d",&min);
    printf("Enter a maximum value.\n");
    scanf("%d",&max);
    srand(10);
    for(int i = 0; i < 100; i++)
    {
        randoms[i] = (rand() % ((max-min)+1) + min); 
    }
    output();
    lowest();
    highest();
    average();
}

void output()
{
    for(int i = 0; i<101; i++)
    {
        if(i % 10 == 0)
        {
            printf(randoms[i]);
            printf("\n");
        }
        else
        {
            printf(randoms[i]);
        }
    }
}

int lowest()
{
    for(int i = 0; i > 101; i++)
    {
        if(randoms [i] < min) //finds the lowest value of array
        {
            min = randoms[i];
        }
    }
    printf("The lowest value is: %d", %min);
    return min;
}

int highest()
{
    for(int i = 0; i > 100; i++)
    {
        if(randoms[i] > max)
        {
            max = randoms[i];
        }
    }
    printf("The highest value is: %d", &max);
    return max;
 }

float average()
{
    int sum = 0;
    int count = 0;
    for(int i = 0; i > 100; i++)
    {
        count++;
        sum += randoms[i];
    }
    average = sum/count;
    printf("The average value is: %d", &average);
    return average;
}
Sosa
  • 39
  • 5
  • 2
    For one, you apparently don't understand how [`printf()`](http://en.cppreference.com/w/c/io/fprintf) works. Read the documentation. `printf(randoms[i]);` certainly doesn't fit the bill. Also, unless you intended on integer division to be done, `average = sum/count;` isn't likely to do what you think it does. And stop naming your functions the names of global variables. A function *prototype* is what you need to use. Something tells me `for(int i = 0; i > 101; i++)` isn't going to do much either. – WhozCraig Mar 23 '15 at 04:02
  • 2
    With so many errors, it's hard to say where to start. My suggestion: take small steps in creating the program and making sure that each step works as it should. – R Sahu Mar 23 '15 at 04:03
  • Perhaps you could ask a question about the error which confuses you, and we could then have a discussion about it... – autistic Mar 23 '15 at 04:22
  • @sharon the compiler doesn't matter here, actually. – Hi-Angel Mar 23 '15 at 04:38

1 Answers1

0

The first is that the functions should be declared before they're used. I see you tried to do it, but you declared them as variables, it is wrong. The second: learn how to work with printf function, there's too many errors with it. Here:

printf("The highest value is: %i", &max);

You're trying to print an address of variable instead of the variable itself.

Also, since you're programming in C, but not C++, you can't declare variable somewhere in the middle of a function, so you should been declared the i's that are used in cycles at the beginning.

Also you used one variable in the function average() that had the same name as a function, and thus conflicted. I redeclared it in the body of a function like just float ret.

It's how you code should look like to compile (but note, that I didn't checked for logical errors, just made it to compile).

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

int min, max;
int randoms [100];

void output();
int lowest();
int highest();
float average();

int main()
{
    int i=0;
    printf("Enter a minimun value.\n");
    scanf("%d",&min);
    printf("Enter a maximum value.\n");
    scanf("%d",&max);
    srand(10);
    for(; i < 100; i++)
    {
        randoms[i] = (rand() % ((max-min)+1) + min);
    }
    output();
    lowest();
    highest();
    average();
    return 0;
}

void output()
{
    int i=0;
    for(; i<101; i++)
    {
        if(i % 10 == 0)
        {
            printf("%i\n", randoms[i]);
        }
        else
        {
            printf("%i\n", randoms[i]);
        }
    }
}

int lowest()
{
    int i=0;
    for(; i > 101; i++)
    {
        if(randoms [i] < min) //finds the lowest value of array
        {
            min = randoms[i];
        }
    }
    printf("The lowest value is: %i", min);
    return min;
}

int highest()
{
    int i=0;
    for(; i > 100; i++)
    {
        if(randoms[i] > max)
        {
            max = randoms[i];
        }
    }
    printf("The highest value is: %i", max);
    return max;
}

float average()
{
    int sum = 0;
    int count = 0;
    float ret;
    int i=0;
    for(; i > 100; i++)
    {
        count++;
        sum += randoms[i];
    }
    ret = sum/count;
    printf("The average value is: %f", ret);
    return ret;
}
Hi-Angel
  • 4,933
  • 8
  • 63
  • 86
  • Thank you so much, I got rid of all my errors after seeing this and going back over my code. Now I have a logical question, how would I allow people to search for an integer in the array? – Sosa Mar 23 '15 at 07:01
  • @Sosa if it solved your problem, then accept the answer. As of your comment: well, if I understood right, you first get an integer from a user. Next, since what you got is actually a text, you have to [convert it to an integer](http://stackoverflow.com/questions/7021725/converting-string-to-integer-c). Now you got a number, next simply do a cycle to seek for this number in the array. – Hi-Angel Mar 23 '15 at 07:07
  • @Sosa ah, right, you can use `scanf` as you did, then you get a number without a converting. Then just do a cycle over an array, and seek for this number. – Hi-Angel Mar 23 '15 at 07:11
  • @Sosa something like `int i = 0; for (;i < sizeof(randoms); ++i){ if (randoms[i] == theNumISeek) printf("Hurray");}` – Hi-Angel Mar 23 '15 at 07:16