need a hand XD

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

need a hand XD
  1. Unread #1 - Jul 9, 2010 at 2:57 AM
  2. PsychicFreak
    Joined:
    Jun 6, 2008
    Posts:
    53
    Referrals:
    0
    Sythe Gold:
    0

    PsychicFreak Member

    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++
     
  3. Unread #2 - Jul 9, 2010 at 12:28 PM
  4. blindkilla
    Joined:
    Jun 22, 2005
    Posts:
    1,896
    Referrals:
    0
    Sythe Gold:
    6
    Discord Unique ID:
    282000633404456960
    Discord Username:
    sogord

    blindkilla Guru
    $25 USD Donor New

    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
     
  5. Unread #3 - Jul 10, 2010 at 3:02 AM
  6. Barz
    Joined:
    May 24, 2010
    Posts:
    753
    Referrals:
    0
    Sythe Gold:
    0

    Barz Apprentice
    Banned

    need a hand XD

    is this c or c++?
     
  7. Unread #4 - Jul 10, 2010 at 7:45 AM
  8. PsychicFreak
    Joined:
    Jun 6, 2008
    Posts:
    53
    Referrals:
    0
    Sythe Gold:
    0

    PsychicFreak Member

    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
     
  9. Unread #5 - Jul 10, 2010 at 9:25 AM
  10. blindkilla
    Joined:
    Jun 22, 2005
    Posts:
    1,896
    Referrals:
    0
    Sythe Gold:
    6
    Discord Unique ID:
    282000633404456960
    Discord Username:
    sogord

    blindkilla Guru
    $25 USD Donor New

    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);
    }
    
     
  11. Unread #6 - Jul 10, 2010 at 9:33 AM
  12. ET Phone Home
    Joined:
    Sep 8, 2008
    Posts:
    663
    Referrals:
    1
    Sythe Gold:
    0

    ET Phone Home Apprentice

    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 >

Users viewing this thread
1 guest


 
 
Adblock breaks this site