Thread: Sum up the largest and smallest ones

  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
    136
    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
    casual coder spaghetticode's Avatar
    Join Date
    Jan 2011
    Location
    Hesse, Germany
    Posts
    259
    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.
    Hardware: Intel® Core™ i7-3630QM CPU @ 2.40GHz × 8 | Acer Aspire V3-771G
    OS: Windows 10 Home

  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
    casual coder spaghetticode's Avatar
    Join Date
    Jan 2011
    Location
    Hesse, Germany
    Posts
    259
    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 | Acer Aspire V3-771G
    OS: Windows 10 Home

  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?

  9. #9
    casual coder spaghetticode's Avatar
    Join Date
    Jan 2011
    Location
    Hesse, Germany
    Posts
    259
    Right.
    Hardware: Intel® Core™ i7-3630QM CPU @ 2.40GHz × 8 | Acer Aspire V3-771G
    OS: Windows 10 Home

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, 05:08 PM
  2. Replies: 22
    Last Post: 05-29-2009, 05:44 PM
  3. largest and smallest
    By eldemonio in forum C Programming
    Replies: 9
    Last Post: 10-15-2007, 02:00 PM
  4. Largest / Smallest (5 integers)
    By Ripley in forum C Programming
    Replies: 4
    Last Post: 10-09-2005, 08:58 PM
  5. Replies: 17
    Last Post: 03-23-2003, 05:00 PM