Hello
I have to create program in which user has to choose how many numbers he would like to enter for example 3. then program ask about these numbers.(Array needs to have space=100) After that program has to add the terminator with value (-1) on the next free space in the array. (that is my main problem).
For example, if the user enters that they have 5 values, the system will then ask them5 times to enter a number to be placed in the arrayOnce the user has finished entering their values, the system should add a -1 in to thenext free element of the array. This will act as a 'terminator'.
in the same program system should ask the user what element of the arraythey want to replace. It should then ask what number they want to put in theirchosen array element.The program should then replace the users chosen array element with the value theyentered.
Main problem which i have i do not know how to use terminator, when i cant declare variables like int /code a[5]={1,2,3,4\-1};, like a[100]; and user has to enter the values. /code
Code:
#include <stdio.h>
#include <ctype.h>
void As5GradeB()
{
int a[100];
char opt;
int i;
float mean;
int sum = 0;
int num;
opt = tolower(opt);
printf("Enter number of elements \n");
scanf("%d", &num);
for (i = 0; i < num; i = i + 1) {
printf("Enter value of the elements%d\n", i + 1);
scanf("%d", &a);
}
while ((opt = getchar()) != '\n' && opt != EOF);
while (opt != 'x') {
printf("\n Please enter your choice from the following menu:\n");
printf("A – Repopulate array \n");
printf("B - Display all values \n");
printf("C – Replace one number \n");
printf("D – Calculate the mean \n");
printf("E - Find largest number\n");
printf("X - Exit the program \n");
scanf("%c", &opt);
opt = tolower(opt);
if (opt == 'a') {
for (i = 0; i < num; i = i + 1) { // reading 5 values from user and storing it in array
printf(" Enter the value number %d\n\n", i + 1);
scanf("%d", &a);
}
printf("The new array is : \n");
for (i = 0; i < num; i++) {
printf("%d\n", a);
}
} else if (opt == 'b') {
printf("You choose to display all values \n\n");
for (i = 0; i < num; i++) {
printf("%d\n", a);
}
} else if (opt == 'c') {
int ele;
int val;
printf("You choose to replace one number\n");
printf("Which element of the array You want to replace ? \n");
scanf("%d", &ele);
printf("What is the replaced value ?");
scanf("\n%d", &val);
for (i = 0; i < 100; i++) {
if (a == ele) {
a = val;
}
}
printf("%d", a);
} else if (opt == 'd') {
sum = 0;
for (i = 0; i < num; ++i) {
sum += a;
}
mean = sum / num;
printf
("Mean = sum of the elements devided on the number of elements\n = %.2f\n",
mean);
}
else if (opt == 'x') {
printf("Thank you for using the program");
break;
} else if (opt == 'e') {
printf("You choosed to find the largest element \n\n");
// storing the largest number to arr[0]
for (i = 0; i < num; ++i) {
if (a[0] < a)
a[0] = a;
}
printf("Largest element = %d\n", a[0]);
} else {
printf("Wrong menu option- try again\n");
}
while ((opt = getchar()) != '\n' && opt != EOF);
}
}