Like Tree4Likes
  • 1 Post By dennis.cpp
  • 2 Post By programmer1
  • 1 Post By dennis.cpp

Sum up the largest and smallest ones

This is a discussion on Sum up the largest and smallest ones within the C Programming forums, part of the General Programming Boards category; Hi! I am learning C programming and I need some help. In my project I have to enter n numbers ...

  1. #1
    Princess of code universe programmer1's Avatar
    Join Date
    Nov 2011
    Location
    Wonderland
    Posts
    38

    Sum up the largest and smallest ones

    Hi!

    I am learning C programming and I need some help. In my project I have to enter n numbers and find the smallest and the largest number. AND I DID THIS!

    But I can not sum up this two values! Could you please help me?

  2. #2
    Registered User
    Join Date
    Nov 2011
    Posts
    130
    You did what?

  3. #3
    Registered User
    Join Date
    Sep 2011
    Location
    Stockholm, Sweden
    Posts
    131
    Show us the code you have so far.

  4. #4
    Princess of code universe programmer1's Avatar
    Join Date
    Nov 2011
    Location
    Wonderland
    Posts
    38

    Post

    Quote Originally Posted by iceaway View Post
    Show us the code you have so far.
    I am sorry, I am the new comer.Next time I will be more careful.

    Code:
    #include<stdio.h> 
    main() 
    { 
    int n,m,i,min,max; 
    
    
    printf("Please enter a positive integer:"); 
    scanf("%d",&n); 
    printf("Enter integers:"); 
    scanf("%d",&m); 
    max=m;
    min=m; 
    for(i=2;i<=n;i++) 
    { 
    scanf("%d",&m); 
    if(m<min) 
    min=m; 
    if(m>max)
    max=m;
    } 
    printf("The smallest number is %d\nThe greatest number is %d\n",min,max);
    scanf("%d %d",&min,&max);
    printf("The sum is : %d\n",min+max);
    }

    The sum part can t work. Please help me!

  5. #5
    language hopper dennis.cpp's Avatar
    Join Date
    Jan 2011
    Location
    Herborn, Hesse, Germany
    Posts
    197
    What is line 22 supposed to do? You're overriding your min and max there. Get rid of that and your summation should work correctly.
    Salem likes this.
    Hardware: Intel® Core™ i7-3630QM CPU @ 2.40GHz × 8
    Operating system: Debian Testing/Jessie / Linux 3.16-2-amd64 / KDE SC 4.14.1
    Compiler: gcc 4.9.1-16 with KDevelop 4.7.0

  6. #6
    Princess of code universe programmer1's Avatar
    Join Date
    Nov 2011
    Location
    Wonderland
    Posts
    38
    Thank you so much, Dennis.cpp. It works I am so happy!!!!!

  7. #7
    language hopper dennis.cpp's Avatar
    Join Date
    Jan 2011
    Location
    Herborn, Hesse, Germany
    Posts
    197
    Do you understand why and what's different now? What had you been trying to achieve with that line 22?
    Hardware: Intel® Core™ i7-3630QM CPU @ 2.40GHz × 8
    Operating system: Debian Testing/Jessie / Linux 3.16-2-amd64 / KDE SC 4.14.1
    Compiler: gcc 4.9.1-16 with KDevelop 4.7.0

  8. #8
    Princess of code universe programmer1's Avatar
    Join Date
    Nov 2011
    Location
    Wonderland
    Posts
    38
    Quote Originally Posted by dennis.cpp View Post
    Do you understand why and what's different now? What had you been trying to achieve with that line 22?
    Yeah , I got it. Because if I use scanf , the program wants new values for min and max. But I already have min and max! So we have to delete scanf! And program takes my first min and max . Is it right?
    dennis.cpp and Salem like this.

  9. #9
    language hopper dennis.cpp's Avatar
    Join Date
    Jan 2011
    Location
    Herborn, Hesse, Germany
    Posts
    197
    Right.
    programmer1 likes this.
    Hardware: Intel® Core™ i7-3630QM CPU @ 2.40GHz × 8
    Operating system: Debian Testing/Jessie / Linux 3.16-2-amd64 / KDE SC 4.14.1
    Compiler: gcc 4.9.1-16 with KDevelop 4.7.0

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Kth largest &kth smallest
    By baffleddreams in forum C Programming
    Replies: 16
    Last Post: 10-01-2010, 06:08 PM
  2. Replies: 22
    Last Post: 05-29-2009, 06:44 PM
  3. largest and smallest
    By eldemonio in forum C Programming
    Replies: 9
    Last Post: 10-15-2007, 03:00 PM
  4. Largest / Smallest (5 integers)
    By Ripley in forum C Programming
    Replies: 4
    Last Post: 10-09-2005, 09:58 PM
  5. Replies: 17
    Last Post: 03-23-2003, 06:00 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21