Thread: Min Max Sum Average median and Mode from an Array....Where to start...Lost

  1. #1
    Registered User
    Join Date
    Apr 2012
    Posts
    6

    Exclamation Min Max Sum Average median and Mode from an Array....Where to start...Lost

    I am very new to C and am needing help getting started with this code, I am needing to find the min max sum average median and mode from the array that ranges from 10 to 50 of 1000 size array. Any help is appreciated...Thanks in advance.
    Code:
    #include <stdio.h>#include <stdlib.h>
    
    
    int main ()
    {
        int mainArray[1000];
        int size = 1000;
        int currPos = 0;
        int randNumber = 0;
        srand(20);
        
        for (currPos = 0; currPos < 1000; currPos++) 
        {
            
            randNumber = rand()%41 + 10; //mix up current positions
        
        }
        
    
    
    
    
    }

  2. #2
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Well first of all you don't have any data in your array. Start by reading some data in.
    Secondly, what is the point of using an array of size 1000 if you are only using 41 elements?
    Thirdly, what are you doing using rand, what does that have to do with anything?
    Lastly, main returns an int, as you yourself have written in your code, so return 0 at the end.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  3. #3
    Registered User
    Join Date
    Apr 2012
    Posts
    6
    I thought that the randNumber command is filling the array. we were instructed to fill the array with random numbers from 10 to 50 and the array size to be 1000

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,505
    Notice that you are assigning to randNumber, not to any element in mainArray.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Apr 2012
    Posts
    6
    Quote Originally Posted by laserlight View Post
    Notice that you are assigning to randNumber, not to any element in mainArray.


    Is this a step in the right direction?
    Code:
    #include <stdio.h>#include <stdlib.h>
    
    
    
    
    int main ()
    {
        int mainArray[1000];
        int size = 1000;
        int currPos = 0;
        int randNumber = 0;
        srand(20);
        
        for (currPos = 0; currPos < 1000; currPos++) 
        {
            mainArray[currPos] = rand()%50
    
    
        return 0;
    
    
    }

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,505
    Yes, it is. I suggest that you get it to compile first, and then start with finding the min.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Apr 2012
    Posts
    6
    Quote Originally Posted by laserlight View Post
    Yes, it is. I suggest that you get it to compile first, and then start with finding the min.
    It runs with no errors but doesnt print anything....

    Code:
    #include <stdio.h>#include <stdlib.h>
    
    
    
    
     main ()
    {
        int mainArray[1000];
        int size = 1000;
        int currPos = 0;
        int randNumber = 0;
        srand(20);
        
        for (currPos = 0; currPos < 1000; currPos++) 
        {
            mainArray[currPos] = rand()%50;
    
    
        return 0;
    
    
    
    
        int minimum, location = 1;
    
    
        minimum = mainArray[10];
        
        for (currPos = 10; currPos < 50; currPos++)
        {
            if (mainArray[currPos] < minimum )
            {
                minimum = mainArray[currPos];
                location = currPos+1;
            }
        }
        printf("Minimum element is present at location number %d and it's value is %d.\n", location, minimum);
        return 0;
    }
    }

  8. #8
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Haha what a joke. I suggest you drop the class mate, or stop writing random crap with no understanding hoping that it will somehow magically work.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  9. #9
    Registered User
    Join Date
    Apr 2012
    Posts
    6
    nevermind on why it isnt printing anything. But it always prints location 1 and the value as 0. Is the array actually being filled with numbers between 10 and 50?

  10. #10
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Oh, it prints does it now? It must be printing in Hogwarts right after that return 0 you have in the middle of the code staring at you like a rapist.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,505
    djay86: I presume that you are a classmate of djay86753 and chose your username as a prank of sorts, but whatever, I shall assume that the both of you are different entities, so kindly stop bickering. The irrelevant posts in this thread shall be deleted.

    Quote Originally Posted by djay86
    Is the array actually being filled with numbers between 10 and 50?
    Print each element of the array to find out.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  12. #12
    Registered User Jugurtha Hadjar's Avatar
    Join Date
    Mar 2012
    Location
    Algiers, Algeria.
    Posts
    3
    Hello,

    -I suggest you start with a smaller array, say 5.
    -Assign each element of this array yourself.
    -Try to find the minimum, use
    Code:
    printf
    in each step to see what's going on and if what you're righting follows your thinking..


    For example, finding the minimum would look like :


    Code:
    #include<stdio.h>
    
    int main(int argc, char *argv[])
    
    
    {
    
        int mainArray[5] = {10, 24, 2, 121, 0};
        int i, min = 0;
    
        for(i = 0; i<=4; i++)
        {
            printf("mainArray[%d] = %d\n", i, mainArray[i]);
        }
     
         printf("\n");
    
        i = 0;
        
        min = mainArray[i];
    
        for(i = 0; i<=4; i++)
        {
    
            if(min > mainArray[i]) min = mainArray[i];
            printf("Minimum for i=%d :%d\n", i,min);
    
    
         }
        
        printf("\nYour final Minimum is %d\n", min);
    
        return 0;
    
    }
    It's a good idea not to put all the "features" all in once. Put one, make it work, debug. Add the other, etc...

    All my best

  13. #13
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,907
    If you use a constant to define the array size, and use it for all your array loops, then you can use Jugurtha's solution to make your program work, and simply change the constant definition to 1000, and your assignment will be done. Literally changing just a few characters.

  14. #14
    Registered User
    Join Date
    Nov 2011
    Location
    Buea, Cameroon
    Posts
    197
    i don't know why some 'nerds' tend to be arrogant to other dudes.....

  15. #15
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    @OP: Do you know what
    Code:
    return 0;
    does? Not sure if it's an accident or you truly don't know its function, but because you left it in, your main function returns before it even gets to the sorting part.

    Also, it's technically included in the for loop, because the extra brace at the bottom makes it compile correctly, but it definitely won't function correctly. How you have it now, it's filling one number, doing the sort, filling another, doing the sort, etc.

    Code:
    minimum = mainArray[10];
    What? Why not just make it equal to zero, instead of picking a random array index that very well might be the largest value in the array. You're making the same mistake on the sorting loop. Do you even understand how loops and arrays work?

    Quote Originally Posted by Nyah Check View Post
    i don't know why some 'nerds' tend to be arrogant to other dudes.....
    What the ........ are you talking about? No offense, but a lot of your replies are completely irrelevant, dumb, and/or re-stating what's been said.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 02-23-2012, 11:02 PM
  2. mean, mode median calculation..
    By naspek in forum C Programming
    Replies: 2
    Last Post: 09-10-2009, 09:15 AM
  3. Mean, Median, and Mode .....code
    By AdamLAN in forum C++ Programming
    Replies: 2
    Last Post: 10-22-2004, 02:29 PM
  4. Finding Mode Median and Mean
    By Ginny Morgan in forum C Programming
    Replies: 3
    Last Post: 05-08-2003, 03:09 PM
  5. median mode
    By bobbydigital in forum C++ Programming
    Replies: 2
    Last Post: 04-12-2003, 08:53 PM