Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Problem in storing data dynamically in linked list

Tags:

c

I'm trying to write a program that reads a maximum of 8 bank account information and store them dynamically in a linked list. I wrote a function to calculate the average of all the entered bank accounts' balances but when I try to call that function no output is produced. can you please help me figure out where the problem is?

#include <stdio.h>

#include <stdlib.h>

//node structure
typedef struct node {
   int no;
   char name[20];
   float total;
   struct node * nextptr;
}
account;

//int for division of total to find the average
static int t;

//adding accounts function
account * addaccount(account * temp) {
   fflush(stdin);
   printf("Enter the account name: \n");
   gets(temp -> name);
   printf("Enter the account number: \n");
   scanf("%d", & temp -> no);
   printf("Enter the account balance: \n");
   scanf("%f", & temp -> total);
   t++; // used for finding the average 
   return temp;
}

// function for calculating the average
float sum(account * temp) {
   float average = 1.0, sum = 0.0;
   int i;
   account * start;
   temp = start;
   for (i = 0; i < t; i++) {
      sum += temp -> total;
      temp = temp -> nextptr;
   }
   average = sum / t;
   return average;
}

int main() {
   int selection;
   account * start = NULL;
   account * save, * temp;
   account * ptr;
   ptr = (account * ) malloc(sizeof(account) * 8);
   do {
      //Menu
      printf("\n1.Adding account\n");
      printf("2.Find the average of the added accounts' balances\n");
      printf("3.Exit\n");
      scanf("%d", & selection);
      switch (selection) {
      case 1: {
         if (ptr == NULL)
            ptr = (account * ) realloc(ptr, sizeof(account));
         save = addaccount(ptr);
         if (start == NULL) {
            start = save;
            start -> nextptr = NULL;
         } else {
            temp = start;
            while (temp -> nextptr != NULL)
               temp = temp -> nextptr;
            temp -> nextptr = save;
            save -> nextptr = NULL;
         }
         break;
      }
      case 2: {
         float avg;
         avg = sum(temp);
         printf("%f", avg);
         break;
      }
      case 3: {
         temp = start;
         while (temp != NULL) {
            free(temp);
            temp = temp -> nextptr;
         }
         break;
      }
      }
   } while (selection != 4);
   return 0;
}
like image 387
Buffer Avatar asked Dec 18 '25 02:12

Buffer


1 Answers

Look here

// function for calculating the average
float sum(account* temp) {
    float average = 1.0, sum = 0.0;
    int i;
    account* start; <<<==== a random value
    temp = start;   <<<=== over write the parameter of this function with a random value
    for (i = 0; i < t; i++) {
        sum += temp->total;
        temp = temp->nextptr;
    }
    average = sum / t;
    return average;
}

Not sure what you are trying to do here - but that is for sure wrong

like image 70
pm100 Avatar answered Dec 20 '25 18:12

pm100



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!