# How to enter a range of values in C (New to coding)

Im making a very simple program just to test myself (I only started learning code a day ago so yeknow). Im just making a program that will take 4 numbers and average them together. It’s a grade point average calculator basically. But something I cant seem to finds out is how to enter a range of numbers. I want to make it so if you get anything lower then a 65 it says “uh oh! Your failing!” but I don’t want to go through and enter another ‘else if’ statement for every single number. Can I get any help on this?

Have you had a look at arrays and for loops? If not you should take a look, I think it should help with your problem. http://www.thegeekstuff.com/2011/12/c-arrays/

sounds like something an array would do just fine. Need an example?

It would be appreciated :)

Here's something that does what you're aiming for I think . Feel free to post more questions as there's a few things on there you might not have come across if you're just starting out.

A lot of that just went right over my head. Then again, the syntax was more advanced than what I know right now. Why didn't you just initialize i inside the first argument in the for loop.

Huh? Don't you just want to use greater/small than operators?

You're right, that would save a line of code and that is what I had initially but my compiler complained and rather than figuring out why I just did it like that. I'll give a quick overview of some of the potentially stranger looking things and why I used them for the benefit of any beginners reading.

#define NAME value : Look through the code, wherever you find NAME replace it with value. Pretty simple but it makes the code more readable and if say the passing grade is later lowered for some reason, you only have to change the value in one place no matter how often you use it.

total/(float)NUM_GRADES: says 'regardless of what type NUM_GRADES is, make it a floating point number' . This is necessary because both total and NUM_GRADES are integers, which means there are no decimal points, which means you always get a whole number which isn't really a bad thing in this case but I usually like to see some decimal points when I divide things.

printf("%.2f", 1.23456789): the 'f' tells print that it should print a floating point number, the .2 means it should only print it to two decimal places.

I'm not sure if that helps or makes things worse, let me know if it makes things worse, haha.

Yeah, look up arrays and C syntax.

Haven't checked gamedev.net recently, but that would be a better place to ask that sort of question. They also have a bunch of books and tutorial references.

I wouldnt really throw yourself into arrays in your first week. Build up to using them. Baby steps, perhaps start with processing single int,float or double values.

//Include the standard header that defines all the

//neat functions we are going to be using in our code.

#include <stdio.h>

//Every C program must have a "main" function. This is

//the function that the OS calls to begin our program.

int main()

{

//We create an array of integers called "values" with four elements

int values[4] = {0, 0, 0, 0};

//We create a integer variable to store the sum of all our values.

int total = 0;

//We create a variable of type float because when dividing we get remainders.

float average = 0.0f;

//The user end of our application..

printf("Enter 4 values to be averaged\nExample : 65 92 100 86\n:");

//Read the entered values and store them in our array

scanf("%d %d %d %d", &values[0], &values[1], &values[2], &values[3]);

//Add up all the values and...

total = values[0] + values[1] + values[2] + values[3];

//Divide by the number of values

average = total / 4;

//Now time for some humor

if(average < 65)

printf("Uh Oh! You are retarded! :(");

else

printf("Yay! You are slightly less retarded! \\:D/");

return(0);

}

Hope this helps explain some stuff..