Question
Hi, for my Programming class, we need to make sure that our code complies without error for the GCC compiler which conforms to c89 rules
Hi, for my Programming class, we need to make sure that our code complies without error for the GCC compiler which conforms to c89 rules of C. I'm getting an error "'for' loop initial declarations are only allowed in c99 mode." Could you help me figure out, and correct what is wrong in the code to no longer have this compile error? attached it the program.
//This program allows user inputs into an array and will check if three numbers are of the same value
//Chase Tappan 2/11/2018
#include
//The two functions are created: Main and three_numbers.
int three_numbers(int a[], int n, int *number);
int main(){
//We initalize some variables for main.
int n, number=0;
//The user is prompted to enter the length of the array.
printf("Enter the length of the array (Atleast 3): ");
scanf("%d", &n);
//If the array is under 3 numbers long, the program ends.
if(n < 3){
printf("You did not enter an array of appropriate length.");
return 0;
}
//An array is created to hold the numerical elements.
int a[n];
printf("Enter the elements of the array : ");
//a for looop is created to loop for the specified number of digits the user said will be placed in the array.
for(int i=0; i scanf("%d", &a[i]); //We set a variable to store the value outputted by three_numbers (0 or 1) int valid = three_numbers(a,n,&number); //1 = true, else, false. if(valid == 1) printf("There are atleast three numbers with the same value in the array: %d ", number); else printf("This array does not have 3 numbers that are the same. "); } //The function three_numbers is built to count how many instances of a certain number there are. int three_numbers(int a[], int n, int *number) { //Variables are initalized. int count, test = 0; for(int i=0; i { count = 0; //A for loop is used that checks if the values are the same, if they are, it will add one to count. for(int j=i+1; j { if(a[i] == a[j]) count++; //Count is checked to see if it has had the same variable more than twice. if(count == 2) { *number = a[i]; test = 1; break; } if (test == 1) break; } } //we return the test value out of this function which will function as our true or false. return test; }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started