Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I have a code that does a simple task. It asks users how many numbers they want

ID: 3565307 • Letter: I

Question

I have a code that does a simple task. It asks users how many numbers they want to enter, the user enters the number, and the program stores the integers in an array. It then gives the user the option to A) add integers to the array, B) Show the current list of integers , or C) quit the program. What i want to do is add a fourth option, which gives the user the option to delete an integer from the array. The function will prompt the user to enter a number, search the array to see if it contains that student, and if found, delete that number from the array. But i have an error that says 98:1: error: 'for' loop initial declarations are only allowed in C99 mode 102:1: error: 'for' loop initial declarations are only allowed in C99 mode. Can someone help me with this? Much appreciated, Thank you!

#include<math.h>
#include<stdio.h>
#include<malloc.h>
#include<conio.h>
//Function to resize array
int ResizeArray();
//Print array value
void PrintArray();
//Delete an element
void DeleteFromArry();
//Size of array
int size;
//Conditon1: Pointer to Integer
int * array;
int main()
{
//Get Option
int option=0;
size=0;
int k;
//Condtion2 : Get size of arry from user
printf("How many integers would you like to enter? ");
scanf("%d",&size);

// Condtion2 : Set intial size of array from user size
array=(int*)malloc(sizeof(int)*size);
// Get input from user from first time
for(k=0;k<size;k++)
{
printf(" Input the integer: ");
scanf("%d",array+k);
}
//Menu for user
while(1){

printf(" Options: ");
printf(" 1.) add more numbers to the array ");
printf("2.) print the current list of numbers ");
printf("3.) delete a no. from list ");
printf("4.) quit the program ");

scanf("%d",&option);
switch(option){
case 1 :
ResizeArray();break;
case 2:
PrintArray();break;
case 3:
DeleteFromArry();break;
case 4:
printf("Good bye! ");
return 0;
default :
printf("That is an invalid input. Please input an integer corresponding to the given options.");break;
}
}
return 0;
}
//Print array
void PrintArray(){
int k;
printf(" The elements in the array include: ");
for(k=0;k<size;k++){
printf("%d ",*(array+k));
}
}

//Resize array to new value input by user
int ResizeArray()
{
int c;
printf(" How many more integers would you like to add? ");
scanf("%d",&c);
//Reallocate array size
int *newarray=(int*)realloc(array,size+c);
//Set array pointer to newarray
array = newarray;
int k;
// Input for new array size so input from increase :size to size+c
for(k=size;k<size+c;k++)
{
printf(" Input the integer: ");
scanf("%d",array+k);
}
//Update value of size
size= size+c;
}


//delete no. from Arry enter by user
void DeleteFromArry()
{
int c;
int f=0;
printf(" Enter no. you want to delete from Array ");
scanf("%d",&c);

for(int i=0;i<size;i++)
{
if(array[i]==c)
{
for(int j=i;j<size;j++)
array[j]=array[j+1];
printf("No. %d deleted from Array:", c);
size--; //Decreasing the length of the array
f=1;
}
}
if(f==0)
{
printf("No. %d Not present in the array:", c);
}
}

Explanation / Answer

Now it'll work ...

#include<math.h>
#include<stdio.h>
#include<malloc.h>

//Function to resize array
int ResizeArray();
//Print array value
void PrintArray();
//Delete an element
void DeleteFromArry();
//Size of array
int size;
//Conditon1: Pointer to Integer
int * array;
int main()
{
//Get Option
int option=0;
size=0;
int k;
//Condtion2 : Get size of arry from user
printf("How many integers would you like to enter? ");
scanf("%d",&size);

// Condtion2 : Set intial size of array from user size
array=(int*)malloc(sizeof(int)*size);
// Get input from user from first time
for(k=0;k<size;k++)
{
printf(" Input the integer: ");
scanf("%d",array+k);
}
//Menu for user
while(1){

printf(" Options: ");
printf(" 1.) add more numbers to the array ");
printf("2.) print the current list of numbers ");
printf("3.) delete a no. from list ");
printf("4.) quit the program ");

scanf("%d",&option);
switch(option){
case 1 :
ResizeArray();break;
case 2:
PrintArray();break;
case 3:
DeleteFromArry();break;
case 4:
printf("Good bye! ");
return 0;
default :
printf("That is an invalid input. Please input an integer corresponding to the given options.");break;
}
}
return 0;
}
//Print array
void PrintArray(){
int k;
printf(" The elements in the array include: ");
for(k=0;k<size;k++){
printf("%d ",*(array+k));
}
}

//Resize array to new value input by user
int ResizeArray()
{
int c;
printf(" How many more integers would you like to add? ");
scanf("%d",&c);
//Reallocate array size
int *newarray=(int*)realloc(array,size+c);
//Set array pointer to newarray
array = newarray;
int k;
// Input for new array size so input from increase :size to size+c
for(k=size;k<size+c;k++)
{
printf(" Input the integer: ");
scanf("%d",array+k);
}
//Update value of size
size= size+c;
}


//delete no. from Arry enter by user
void DeleteFromArry()
{
int c;
int f=0;
printf(" Enter no. you want to delete from Array ");
scanf("%d",&c);
int i,j;                 //declare var's out of for loop
for(i=0;i<size;i++)
{
if(array[i]==c)
{
for(j=i;j<size;j++)
array[j]=array[j+1];
printf("No. %d deleted from Array:", c);
size--; //Decreasing the length of the array
f=1;
}
}
if(f==0)
{
printf("No. %d Not present in the array:", c);
}
}