Thread: Skipping Conio.h library

  1. #1
    Registered User
    Join Date
    Sep 2011
    Location
    germany
    Posts
    4

    Skipping Conio.h library

    Hey yall,
    ive been working on my homework and have to do a problem, without using the conio.h library (it's not in the course agenda), and don't really know how to go around that.


    The problem reads:
    Write a program you can read up to 20 integers into an array. A negative value ends the input
    loop and the negative value is not part of the array.
    Then by using a switch statement, pressing a (and return) computes the average of the array
    (and prints the result), h prints the highest number in the array, l prints the minimal number in
    the array, s prints the sum of all elements in the array and n prints the number of elements in the
    array. Use functions for each task. The result of these functions must be printed from the main()
    function.
    Hint: The prototype to compute the average looks like this:
    double compute_avg(int arr[], int num);


    That's my work:

    Code:
    #include <conio.h>
    #include <stdio.h>
    double compute_avg(int a[],int n) {
        
        int x,s=0;
        for(x=0;x<n;x++) s+=a[x];
        return (double)s/(n); 
    }
    int compute_max(int a[],int n) {
        
        int x,max=0;
        for(x=0;x<n;x++) if(a[x]>max) max=a[x];
        return max;
        
    }
    int compute_min(int a[],int n) {
        
        int x,min=6200;
        for(x=0;x<n;x++) if(a[x]<min) min=a[x];
        return min;
        
    }
    int compute_sum(int a[],int n) {
        
        int x,sum=0;
        for(x=0;x<n;x++) sum+=a[x];
        return sum;
        
    }
    int main() {
        
        int a[20];
        int i=1;
        
        scanf("%d",&a[0]);
        if(a[0]<0) return 0;
        
        while((a[i-1]>=0)&&(i<20)) {
            
            scanf("%d",&a[i]);
            i++;
        }
        
        if(a[i-1]<0) i--;
        //if last entered value was invalid,we adjust the number of elements
        
        printf(" Enter command and press return. a computes average, h prints highest number,l prints minimum, s prints sum, n prints number of elements.\n");
        
        
        char c;
        
        while(1) {
            
            getchar();
            scanf("%c",&c);
            
            switch(c) {
                    
                case 'a': 
                    printf("The average is: %f\n",compute_avg(a,i));
                    break;
                case 'h':
                    printf("The maximum is: %d\n",compute_max(a,i));
                    break;
                case 'l':
                    printf("The minimum is: %d\n",compute_min(a,i));
                    break;
                case 's':
                    printf("The sum is: %d\n",compute_sum(a,i));
                    break;
                case 'n':
                    printf("The number of elements is: %d\n",i);
    break;
    default:
    printf("Invalid input\n"); 
    
    
    }
    
    }
    
    return 0;
    }

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Quote Originally Posted by mardusj View Post
    Hey yall,
    ive been working on my homework and have to do a problem, without using the conio.h library (it's not in the course agenda), and don't really know how to go around that.
    Everything you're using is from stdio.h. You don't use any functions from conio.h, which is a good thing (it's from a crappy, outdated compiler). All you have to do is remove the line "#include <conio.h>".

    Other than that, and some indentation issues, there are only some minor things:
    1. Print a prompt telling the user how to enter input, "Enter up to 20 numbers, negative to stop: "
    2. You can convert all your number input to a do while loop (see below).
    3. Clear the input buffer at this point, so if the user types too many numbers, you don't screw up your menu input handling.
    4. Print your menu with each choice on it's own line, it's hard to read.
    5. Consider re-printing the menu inside your while(1) loop, so that the user can see the options each time (it may eventually scroll off the screen otherwise).
    6. Consider giving the user an option to quit.
    7. Ditch the gechar call, and change your scanf to scanf(" %c", &c);. Note the space before the %c, it will skip white space (including the leftover newline).
    8. Put a break; in your default case statement.
    Code:
    int i, x, a[20];
    printf("Enter up to 20 numbers, negative to quit: ");
    i = 0;
    do {
        if (scanf("%d", &x) != 1)  // scanf returns the number of successfully converted elements
            printf("Invalid input, integers only\n");
            exit(1);
        if (x >= 0)  // x is just a temp variable to avoid the hassle of dealing with a[i-1] everywhere
            a[i++] = x;
    } while (x >= 0 && i < 20);
    // no need to correct i by subtracting 1 if they entered a negative number to stop (instead of hitting the 20 limit)
    Last edited by anduril462; 10-04-2011 at 05:36 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 05-26-2011, 06:51 AM
  2. Conio.h Library?
    By Duracellis in forum C++ Programming
    Replies: 16
    Last Post: 11-18-2006, 12:19 PM
  3. I need to get the conio library
    By zergdeath1 in forum C++ Programming
    Replies: 6
    Last Post: 10-25-2004, 11:45 AM
  4. DevC++ accepts neither "conio.h" nor "conio.c"
    By Nutshell in forum C Programming
    Replies: 9
    Last Post: 01-18-2003, 04:35 AM
  5. conio.h
    By Jaguar in forum Linux Programming
    Replies: 10
    Last Post: 10-17-2002, 01:12 AM