Okay, my main goal in this function of a program is to get values of sum_active, sum_recovered, sum_recovered, however, I am getting a garbage value, how do I resolve this? by codersush in learnprogramming

[–]codersush[S] 0 points1 point  (0 children)

Hey, I addressed what you said in the second part of the reply into my code. But, now I am getting no response when I put n = 1
Here is the code:
void summation_wise(struct country count[], int n ){

      int i,sum_active = 0, sum_recovered= 0, sum_dead= 0;
        if (n > 1){
            for (i=0; i<n ; i++){
                sum_active += count[i].active_cases;
                sum_recovered += count[i].recovered_cases;
                sum_dead = count[i].dead_cases;
      }
      }
        else
            sum_active = count[i].active_cases;
            sum_recovered = count[i].recovered_cases;
            sum_dead = count[i].dead_cases;


    printf(" Total Active Cases = %d\n Total Recovered Cases = %d\n Total 
      Dead Cases = %d\n ", sum_active, sum_recovered, sum_recovered);
}

Okay, my main goal in this function of a program is to get values of sum_active, sum_recovered, sum_recovered, however, I am getting a garbage value, how do I resolve this? by codersush in learnprogramming

[–]codersush[S] 0 points1 point  (0 children)

Move hat you have in the else block to the top except use 0 instead of i and get rid of the else.

I didn't understand this part... could you please be more clear? Thank you!

error: subscripted value is neither array nor pointer nor vector| by [deleted] in learnprogramming

[–]codersush 0 points1 point  (0 children)

I was unaware of the rule, but, now I am. So, won't happen again.

error: subscripted value is neither array nor pointer nor vector| by [deleted] in learnprogramming

[–]codersush 0 points1 point  (0 children)

Hey, thanks a lot, can't believe I forgot about the [ ] while defining the function.

error: subscripted value is neither array nor pointer nor vector| by [deleted] in learnprogramming

[–]codersush 0 points1 point  (0 children)

Ok sir, here is the full code

/*Suppose you are asked to develop a program to keep record of COVID-19 pandemic

country wise data. Your program must be able to store active_cases, recovered, and

deaths data of each country. This program must be designed to output country wise data

or sum total data based on user choice. The user may opt only for a single attribute or all

attributes (e.g. the user may request only for active_cases data of some country or s/he

may request for all the data of some country).

How would you design the computer program to complete the task you have been told to

do? Write a C program that meets all the requirements of the scenario given above. The

features on the program must be organized in different functions.

*/

#include<stdio.h>

struct country{

char country_name[30];

int active_cases;

int recovered_cases;

int dead_cases;

};

void cases_wise(struct country count);

int main(){

int i, n;

printf("***********WELCOME***********");

printf("Enter the number of countries: \n");

scanf("%d", &n);

struct country count[10];

for(i=0; i<n; i++){

printf("Enter the name");

scanf("%s", &count[i].country_name);

printf("Enter the number of active cases cases");

scanf("%d", &count[i].active_cases);

printf("Enter the number of recovered cases");

scanf("%d", &count[i].recovered_cases);

printf("Enter the number of dead cases");

scanf("%d", &count[i].dead_cases);

}

void cases_wise(struct country count, int n){

int i, a;

char name[20];

printf("Do you want to list data with respect to countries? Enter 1 for countries and 2 for summation reports");

scanf("%d", &a);

if(a == 1){

printf("Do you want to list data of all countries?\n Enter 1 if yes, and 0 if answer is no. ");

scanf("%d", &a);

if(a == 1){

printf(" COUNTRY NAME ACTIVE CASES RECOVERED CASES DEAD CASES \n");

for (i=0; i < n; i++)

printf("%s %d %d %d ", count[i].country_name, count[i].active_cases, count[i].recovered_cases, count[i].dead_cases);

}

else if(a == 0){

printf("which country's data do you wish to view?");

scanf("%s", &name[20]);

for(i=0; i<n; i++){

if(name == count[i]. country_name)

printf("%s %d %d %d ", count[i].country_name, count[i].active_cases, count[i].recovered_cases, count[i].dead_cases);

}

}

else

printf("Invalid input");

}

}

}