c - Passing variable through switch statement with functions -


i attempting learn how pass variables through menu using functions. problem is, @ no point taught how so. can imagine, variables enter in first menu function utilize in cases/other functions such

  if (count==0)   {     low = number;      high = number;      count++;      sum = number;   }   else   {     if (number < low)       number = low;      if (number > high)       high = number;      count ++;      sum += number;   } 

will not pass through function 2, more knowledgeable c realize. not work in int main either. how 1 go defining user entered numbers,highest,lowest,etc. other functions? here have far, loop , menu works fine.

#include<stdlib.h> #include<stdio.h>  int menuchoice() {   int choice;    printf("1.enter number\n");   printf("2.display highest number entered\n");   printf("3.display lowest number entered\n");   printf("4.display average of numbers entered\n");   printf("5.quit\n");    printf("enter choice:   ");   scanf("%i", &choice);    return choice; }  int function1() {   int number;    printf("enter number:\n");   scanf("%i", &number);    return number; }  int function2() {  }  int function3() {  }  int function4() {  }  int main() {   int quit = 0;   while (quit != 1)   {     int menu;      menu = menuchoice();     switch (menu)     {     case 1:       function1();       break;     case 2:       function2();       break;     case 3:       function3();       break;     case 4:       function4();       break;     case 5:       quit = 1;       break;     default:       printf("please enter 1 through 5\n");      }   }   return 0; } 

let's @ ways can improve code.

  1. naming functions - functions should given clear, descriptive names allow not familiar program understand do. names function1 , function2 don't achieve this.

  2. use appropriate data structures - sounds you're trying keep track of of numbers user entered, don't have way of doing that. best way array, container holds other values.to keep track of numbers have far, let's make 2 variables - array stores numbers, , integer keeps track of how many numbers have been entered. now, let's let user enter 100 numbers. writing int arr[100]; , int numcount = 0; @ top of program. quick side note - called global variable - it's not idea, won't worry now.

  3. separate code appropriate functions - job of in function1. performs task well, number user , return it. let's use number. after case 1, let's write

    arr[numcount] = function1(); numcount += 1;

    this sets first unused entry in array entered number, increased counter number of elements have.

  4. don't perform unnecessary computation - let's think how can implement our highest , lowest functions. 1 way go through entire array each time function called , keep track of highest number have seen. work, end repeating lot of work. if each time new number, update current highest , lowest number have seen?

  5. use loops scan through arrays - calculate average, use loop. documentation if don't understand it.

i think can see how extend thinking calculating average of entered values. here's have after modifying - http://pastie.org/10285795.


Comments

Popular posts from this blog

android - Gradle sync Error:Configuration with name 'default' not found -

java - Andrioid studio start fail: Fatal error initializing 'null' -

html - jQuery UI Sortable - Remove placeholder after item is dropped -