Thread: find the largest element

  1. #1
    Registered User
    Join Date
    Dec 2017
    Posts
    20

    find the largest element

    So first of my program is written in a foreign language, sorry about that.
    What i'm trying to program is a calculator that sums upp numbers. And the program will terminate when the user writes a negative number.
    I have been able to construct the first part of the program. the part which sums up float/int numbers. but where i have run into a problem is the second part, which is to find the largest number.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    
        int main ()
    {
        float tal, summan=0, max=0;
        int i;
        for (;;)
        {
        printf("Skriv in ett tal att summera: ");
        scanf("%f", &tal);
            if (tal>=0) {
                summan+=tal;
                printf("Summan är: %.2f\n", summan);
            }
            else break;
        }
        for (i=0; i<tal; i++)
        {
            if (max<tal)
            {
                max=tal;
    printf("Det största inmatade talet var %.2f\n", max);
            }
        }
        return 0;
    }

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Why do you think you need this for loop?

    Code:
    for (i=0; i<tal; i++)
    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    You need to work on your code presentation skills as well.
    Indentation style - Wikipedia
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Apr 2018
    Posts
    2
    You want to record the maximum while you are reading numbers from the user, there's nothing you can do after they've been processed.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 11
    Last Post: 09-28-2013, 02:20 AM
  2. Replies: 3
    Last Post: 01-30-2013, 09:30 AM
  3. get the largest element from the array and print it
    By casperghost in forum C Programming
    Replies: 2
    Last Post: 12-27-2009, 06:22 PM
  4. Find largest and second largest number (help)
    By Arkon in forum C++ Programming
    Replies: 6
    Last Post: 01-20-2006, 11:21 PM
  5. Finding largest element in array
    By Chaplin27 in forum C++ Programming
    Replies: 2
    Last Post: 04-12-2005, 09:18 PM

Tags for this Thread