1) If you're new to coding then you might want to look into python as its syntax is much easier to read/follow.
I want to write a code for evaluating average result of exam marks.
2) So the average of something is as follows: (n1 + n2 + n3) / 3; the sum of all the numbers divided by the amount of numbers there are.
So it sounds like you want a program that asks the user for a series of numbers and returns the average.
I want to include one function which will repeatedly ask for the correct range of marks defined by the principles rather the impossible.
In this case, marks mustn't be smaller than zero or greater than 100.
3) The function asks for a specified range in a loop. The loop will keep going as long as the user inputs an appropriate number. If the user accidentally enters a letter then an error message comes up. Is this accurate?
Here is how I would do it:
Code:
#include <stdio.h>
void my_function() {
float result = 0.0f ;
while( result >= 0 && result <= 100 ) { // This loop will keep going as long as 0 <= result <= 100
printf( "Enter exam result (0-100): ") ;
scanf( "%f", &result ) ; // %d for whole numbers. %f is for floats (decimal numbers)
printf( "You entered: %f\n", result ) ;
}
}
int main() {
my_function() ;
}
4) I'm mainly a c++ developer so that's what I'm familiar with. Here is how you would handle errors (if the user entered a letter instead of a number)
Code:
#include <iostream>
void my_function() {
float result = 0.0f ;
while( result >= 0 && result <= 100 ) { // This loop will keep going as long as 0 <= result <= 100
std::cout << "Enter exam result (0-100): " ;
std::cin >> result ;
if (std::cin.fail()) { // If user entered a letter (error) the following block executes
std::cout << "You did not enter a number!!\n" ;
break ;
}
std::cout << "You entered: " << result << '\n' ;
}
}
int main() {
my_function() ;
}