Adblock breaks this site

need a hand XD

Discussion in 'Programming General' started by PsychicFreak, Jul 9, 2010.

  1. PsychicFreak

    PsychicFreak Member

    Joined:
    Jun 6, 2008
    Posts:
    53
    Referrals:
    0
    Sythe Gold:
    0
    need a hand XD

    guys how can i create a c program that will ask for 3 numbers from the user?
    btw im using dev-C++
     
  2. blindkilla

    blindkilla Guru
    $25 USD Donor New

    Joined:
    Jun 22, 2005
    Posts:
    1,896
    Referrals:
    0
    Sythe Gold:
    6
    Discord Unique ID:
    282000633404456960
    Discord Username:
    sogord
    need a hand XD

    Here is a simple program

    Code:
    #include <stdio.h>
    
    main() {
       int numbers[3];
       int i;
    
       for (i = 0; i < 3; i++) {
           printf("Please enter a number");
           scanf("%d", &numbers[i]);
       }
    
    }
    
    In case you're wondering what the "%d" is, you can read up here on different format specifiers. http://www.mekong.net/tech/printf.htm
     
  3. Barz

    Barz Apprentice
    Banned

    Joined:
    May 24, 2010
    Posts:
    753
    Referrals:
    0
    Sythe Gold:
    0
    need a hand XD

    is this c or c++?
     
  4. PsychicFreak

    PsychicFreak Member

    Joined:
    Jun 6, 2008
    Posts:
    53
    Referrals:
    0
    Sythe Gold:
    0
    need a hand XD

    ohw tnx. then what should i do to calculate the total of the three numbers and the average?

    im trying to learn this lesson :p pls help me hehe
     
  5. blindkilla

    blindkilla Guru
    $25 USD Donor New

    Joined:
    Jun 22, 2005
    Posts:
    1,896
    Referrals:
    0
    Sythe Gold:
    6
    Discord Unique ID:
    282000633404456960
    Discord Username:
    sogord
    need a hand XD

    That's in c.

    You can do two a couple things, loop through the array again adding the numbers, add them right after they input them, or just add them out of the loop. I'll do it the second way. Then average them out after the loop.

    Code:
    #include <stdio.h>
    
    main() {
       int numbers[3];
       int total = 0;
       double average;
       int i;
    
       for (i = 0; i < 3; i++) {
           printf("Please enter a number");
           scanf("%d", &numbers[i]);
           total += numbers[i];
       }
    
       /* sizeof returns the number of bytes in a variable, with this we can get the amount of elements */
       average = total / (sizeof(numbers) / sizeof(numbers[0])); 
    
       printf("The total is %d.  The average is %f.", total, average);
    }
    
     
  6. ET Phone Home

    ET Phone Home Apprentice

    Joined:
    Sep 8, 2008
    Posts:
    663
    Referrals:
    1
    Sythe Gold:
    0
    need a hand XD

    Code:
    #include <iostream>
    #include <conio.h>
    using namespace std;
    int main()
    {
    int num[3];    
    // input VVVV
    for (int b=0;b<3;b++)
    {
        cout << "Enter a number\n";
        cin >> num[b];
    }
    // computation of total
    int total = num[0]+num[1]+num[2];
    // computation of average
    int average = total/3;
    //assuming you want printout of this information?
    cout << "The total of your three numbers is " << total << ", and the average is " << average <<"\n";
    system("Pause");
     return 0;
    }
    
    changing int average to double average would probably be more reasonable, as most averages will contain a fraction. Also I used two different libraries, I'm a pretty simplistic programmer so this might not be the best method. Also I use system("Pause"; at the end to wait for a character.
     
< [Charny Coding] Tutorials List [Video] | How to make a text spammer >


 
 
Adblock breaks this site