[SOLVED] Get the value from function array

Issue

I have this question in the assignment and would love if anybody can help:

write a program to store integers in an array of size 10, initialize your array in a function Get_value() where the user will input 10 integers to fill the array. The program then print a menu given below for the user and must be able to perform various function that the user chooses ..the user commands are :
D display all non zero value in the array
T Display the total
R display all numbers in reverse order
Q quit the program
in c++
I tried to write but I didn’t get the exact answer

#include <stdio.h>
int get_value();
int display();
void total(void);
int main ()
 {
int get_value[10];  
int i;
char c=0;

for(i=0;i<=9;i++){
    printf("Enter The value of get_value[%d]\n",i);
    scanf("%d",get_value[i]);
}

printf(" D to display all non-zero values in the array \n choose T to display the  total \n choose R to display all the number in reverse order \n choose Q to quit the program \n");
scanf("%d",&c);

if(c == D){
        display();
}
else if(c== T){
    total();
}
   return 0;

   }
int display()
{
    int i,get_value[10];
    for(i=0;i<=9;i++){
        if(get_value[i]!=0)
            printf("%d",get_value[i]);
    }
    return 0;
}
void total(void)
{
    int i,sum=0;
    for(i=0;i<=9;i++){
        sum+=i;
    }
        printf("%d",sum);
}

Solution

Note that:

if (c == D)

should be:

if (c == 'D')

Similarly:

else if (c == T)

should be:

else if (c == 'T')

There are various other problems too, e.g. as already pointed out int he comments:

scanf("%d",&c);

is incorrect – it should be:

scanf("%c",&c);

although:

c = getchar();

is probably better.

Answered By – Paul R

Answer Checked By – Willingham (BugsFixing Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *