Thread: Finding the maximum in an array

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    14

    Finding the maximum in an array

    Alright, newb question, bare if you can =P, I do have an example of my work, dont worry im not asking you to do my homework. Alright, I need the user to input 10 intergers, store them in a float array and output the max, hes my work so far.

    Code:
    #include<stdio.h>
    #define size 10
    
    main()
    {
    int i, A[size];
    float max;
    
    printf("Enter 10 intergeers: ");
    for (i=0; i<size; i++)
            scanf("%f", &A[i]);
    
    for (i=0; i<size; i++){
    if (A[i] > max)
            max == A[i];
    }
    
    printf("The max is %.0f \n", max);
    
    system("PAUSE");
    
    return 0;
    
    }
    obviously it just spits out some nonsense. Not sure what im doing wrong. Thanks for any help.

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Code:
    int i, A[size];
    scanf("%f", &A[i]);
    Use %d or %i to read in ints. %f is for floats. [edit=3] Actually, since you made max a float, you might want to make A a float too instead. In that case you would leave the %f alone.

    In future, you can look up the scanf() function if you suspect that something's wrong with it. http://www.cppreference.com/stdio/scanf.html [/edit]

    [edit]
    Code:
    max == A[i];
    Your compiler might warn you about this if you turned on warnings (and the %f vs %d too). You meant to use a single '=' for assignment. [/edit]

    [edit=2]
    Code:
    main()
    You should use int main() for new programs.
    Code:
    int main()
    or
    Code:
    int main(void)
    http://faq.cprogramming.com/cgi-bin/...&id=1043284376 [/edit]
    Last edited by dwks; 11-14-2006 at 03:58 PM.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    Code:
    system("PAUSE");
    to just pause the process system() is not a good idea cos it takes much of OS time to process it. Its better to use a getchar which is nothing but waitng till a char on a key board is entered which seems like it is been paused

    Instead
    Code:
    getchar();

    ssharish2005

  4. #4
    Registered User MacNilly's Avatar
    Join Date
    Oct 2005
    Location
    CA, USA
    Posts
    466
    The problem w/ your code is that max has not been initialized.

    Also learn the difference between the assignment operator "=" and the logical equals operator "=="

  5. #5
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    Set max to the first element in the array. Otherwise you just get nonsense, as you said (the uninitialized value of max).
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Finding a hyphen in an array.
    By omnificient in forum C Programming
    Replies: 7
    Last Post: 06-17-2008, 02:31 AM
  2. finding size of empty char array
    By darsunt in forum C Programming
    Replies: 12
    Last Post: 05-30-2006, 07:23 PM
  3. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  4. Finding greatest element of an array
    By Unregistered in forum C Programming
    Replies: 4
    Last Post: 12-10-2001, 04:30 PM
  5. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 09:48 AM