Thread: Need some help with arrays

  1. #1
    Registered User
    Join Date
    Jan 2015
    Posts
    4

    Arrow Need some help with arrays

    Hello everyone i've got a project that i must complete and i have a few problems.I have to make a program that must enter 100 elements for an array and display's it.Then I have to get the biggest number of every 5 elements into array1 and all other elements into array2.And find how many times array2 contains the number 5.
    I've done a menu and i've done the option to enter array to work with in the first place.My problem occurs when i try to find the biggest number of every 5 elements and write it.
    Help would be greatly appreciated
    Code:
    #include <visual_2013.h>
    #include <stdio.h>
    #include <stdlib.h>
    int main()
    {
        int i, n, ch, max, count, arr[100], hrr[];
        char *menu[] = { "MENU",
            "1. Enter new array",
            "2. Edit an existing array",
            "3. End"};
        do
        {
            system("cls"); 
            for (i = 0; i < 4; i++)
                printf("\n%s\n", menu[i]);
            printf("\n\nWhat would you like to do: ");
            scanf("%d", &ch);
            switch (ch)
            {
            case 1:
                printf("You chose to enter a new array !\n"); 
                for (i = 0; i < 100; i++)
                {
                    system("cls");
                    printf("%d element will be equal to :", i);      /*Enters an element value*/
                    scanf("%d", &arr[i]);
                }
                printf("\nThe current array has values of :\n");     /*Prints the current array values*/
                for (i = 0; i < 100; i++)
                {
                    printf("%d,", arr[i]);
                }
                for (i = 0; i < 20; i++)
                {
                    for (n = 5; n < i; n++)
                    {
                        max = arr[i];
                            if (arr[i]>max)
                            printf("Success");
                    }
                }
                system("pause");
                break;
            case 2:
                for (i = 0; i < 100; i++)
                {
                    printf("%d,", hrr[i]);
                }
                system("pause");
                break;
            case 3:
                break;
            default:
                printf("\nInvalid option.\n");
                system("pause");
            }
        } while (ch != 3);
        system("pause");
        return 0;
    }
    Last edited by isNomad; 01-07-2015 at 07:16 PM. Reason: mistake

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by isNomad View Post
    My problem occurs when i try to find the biggest number of every 5 elements and write it.
    So, what have you tried? Your code provides no evidence of any attempt to do that.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    Registered User
    Join Date
    Jan 2015
    Posts
    4
    I tried making a for which will loop the array 20 times and a second for to check every 5 numbers for the biggest one but this is not working.I have some problem in the logic and i am not sure how to do it.
    Last edited by isNomad; 01-07-2015 at 07:15 PM. Reason: mistake

  4. #4
    Registered User
    Join Date
    Jun 2011
    Posts
    88
    at line 38 WHEN will arr[i] be greater than max ???
    you have problems at lines 35 and 37
    also how big is hrr ??

  5. #5
    Registered User
    Join Date
    Jan 2015
    Posts
    4
    I assume that in hrr the biggest elements will be stored so 20.

  6. #6
    Registered User
    Join Date
    Jun 2011
    Posts
    88
    Quote Originally Posted by isNomad View Post
    I assume that in hrr the biggest elements will be stored so 20.
    you might want to tell the compiler that

  7. #7
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    isNomad:
    You seemed to have started programming before you were quite sure of how to solve the problem. You should work through several examples with paper and pencil first. If you can't solve a problem, there is no way you can program a computer to solve it. Start with a smaller example, an array of 15 numbers, and pick the largest of each group of 3, putting the other elements in a second array.

    In your small example, you will notice that you are picking 1 out of every 3 elements, or 1/3 of the elements. You have 15 total elements, so your array will have 1/3 * 15 = 15 / 3 = 5 elements in it. So 15 elements in the main array, minus the 5 for the "largest from each group" array, leaves 10 in your "other numbers" array. Those simple calculation will help you figure out how big to make the different arrays in your program.

    I also suggest you use a better name than hrr. WTF is hrr anyways? It reminds me of the sound my dog makes when he "sighs". How about calling it largest_from_each_group. Yes, it's a bit longer, but it's much, much clearer. Never sacrifice clarity to save a few keystrokes.

    One other thing that would help, stop using magic numbers (Magic number (programming) - Wikipedia, the free encyclopedia). Instead, use constants with descriptive names. This goes along with good variable names: it makes the code clear. Also, it allows you to adapt your program to work with different array sizes, groups, etc. Let's apply this to your code:
    Code:
    #define NUM_ELEMENTS 100
    #define GROUP_SIZE 5
    #define NUM_LARGEST_ELEMENTS (NUM_ELEMENTS / GROUP_SIZE)
    #define NUM_REMAINING_ELEMENTS (NUM_ELEMENTS - NUM_LARGEST_ELEMENTS)
    
    // in main, declare your arrays with sensible names:
    int all_elements[NUM_ELEMENTS];
    int largest_from_each_group[NUM_LARGEST_ELEMENTS];
    int remaining_elements[NUM_REMAINING_ELEMENTS];
    
    // whenever you use a loop to process one of those arrays, or to search a group, make sure to use the right constant
    for (i = 0; i < NUM_LARGEST_ELEMENTS; i++)
        for (j = 0; j < GROUP_SIZE; j++)
            ...
    See how much easier it is to read your code? If it's easy to read, it's harder to make mistakes, and easier to find/fix them when you do.

    EDIT:
    I don't have a compiler handy, so the above code is untested. Also, it assumes that NUM_ELEMENTS is a perfect multiple of GROUP_SIZE. A little clever (but fairly simple) math could work around that if you need/want.
    Last edited by anduril462; 01-07-2015 at 08:50 PM.

  8. #8
    Registered User
    Join Date
    Jan 2015
    Posts
    4
    So i've redone most of the code and made a few tests.But my current problem is that the code in the function largestElements that finds the largest of every 5 elements works without problems when its not inside of a function.It just crashes when i choose option 2.I dont know what causes these crashes.If someone can tell me things i should check or correct would be great
    Code:
    #include <visual_2013.h>
    #include <stdlib.h>
    #include <stdio.h>
    
    #define ARRAY_SIZE 10
    #define MAX_SIZE (ARRAY_SIZE / 5)
    #define REST_SIZE (ARRAY_SIZE - MAX_SIZE)
    void enterArray(a);
    void largestElements(int array[], int max[], int rest[]); 
    int main()
    {
        int array[ARRAY_SIZE];
        int rest[REST_SIZE];
        int max[MAX_SIZE];
        int option = 0, i = 0, n = 0, surplus = 0, a;
        char *menu[] = { "MENU",      /*menu initializa*/
            "1.Enter new array",
            "2.Edit existing array",
            "3.End" };
        do {
            system("cls");
            for (i = 0; i < 4; i++)              /*switch options*/
                printf("\n%s\n", menu[i]);
            printf("\n\nChoose an option from [1-3]: ");
            scanf("%d", &option);
            switch (option)
            {
            case 1:
                system("cls");
                printf("You chose to enter a new array!\n");
                for (i = 0; i < ARRAY_SIZE; i++)
                {
                    printf("\nElement(%d) will be equal to : ", i + 1);      /*Entering values for the array*/
                    scanf("%d", &array[i]);
                }
                printf("You successfully entered a new array.\n");
                printf("\nThe current array has values of :\n");     /*Prints the values of the array*/
                for (i = 0; i < 10; i++)
                {
                    printf("%d,", array[i]);
                }
    
                system("pause");
                break;
            case 2:
                largestElements(array[ARRAY_SIZE], max[MAX_SIZE], rest[REST_SIZE]);
                break;
            case 3:
                break;
            default:
                printf("Enter a valid option");
                system("pause");
            }
        } while (option != 3);
        system("pause");
        return 0;
    }
    void enterArray(int array[])                                                 /*Function for entering values for the array*/
    {
        int i;
        printf("Vuvedi elementite na masiva ((100) elementa): \n");
        for (i = 0; i < ARRAY_SIZE; i++)
        {
            printf("a[%d]=", i);
            scanf("%d", &array[i]);
            printf("\n");
        }
    
    }
    void largestElements(int array[], int max[], int rest[])                      /*Function which determines the largest element of every 5*/
    {
        int i, surplus, n;
        for (i = 0; i < MAX_SIZE; i++)
        {
            max[i] = array[i * 5];
            for (n = ((i * 5) + 1); n < ((i * 5) + 5); n++)
            {
                if (array[n] > max[i])
                {
                    rest[surplus] = max[i]; surplus++;
                    max[i] = array[n];
    
                }
                else
                {
                    rest[surplus] = array[n]; surplus++;
                }
    
            }
        }
        printf("main array :\n");
        for (i = 0; i < ARRAY_SIZE; i++)
        {
            printf("Element number %d = %d\n", i + 1, array[i]);
        }
        printf("max array :\n");
        for (i = 0; i < MAX_SIZE; i++)
        {
            printf("MAX number %d = %d\n", i + 1, max[i]);
        }
        printf("Rest :\n");
        for (i = 0; i < REST_SIZE; i++)
        {
            printf("surplus %d = %d\n", i + 1, rest[i]);
        }
    }
    Last edited by isNomad; 01-10-2015 at 11:43 AM.

  9. #9
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Compile at the maximum warning level. It looks like you're using VC++. I *think* the flag is /Wall, but you would need to double check. Here's what I get when I do that:
    Code:
    $ make foo
    gcc -Wall -ggdb3 -pedantic -std=gnu99 -O0 -o foo foo.c -lm -lpthread -lrt
    foo.c:7:1: warning: parameter names (without types) in function declaration [enabled by default]
     void enterArray(a);
     ^
    foo.c: In function ‘main’:
    foo.c:45:17: warning: passing argument 1 of ‘largestElements’ makes pointer from integer without a cast [enabled by default]
                     largestElements(array[ARRAY_SIZE], max[MAX_SIZE], rest[REST_SIZE]);
                     ^
    foo.c:8:6: note: expected ‘int *’ but argument is of type ‘int’
     void largestElements(int array[], int max[], int rest[]); 
          ^
    foo.c:45:17: warning: passing argument 2 of ‘largestElements’ makes pointer from integer without a cast [enabled by default]
                     largestElements(array[ARRAY_SIZE], max[MAX_SIZE], rest[REST_SIZE]);
                     ^
    foo.c:8:6: note: expected ‘int *’ but argument is of type ‘int’
     void largestElements(int array[], int max[], int rest[]); 
          ^
    foo.c:45:17: warning: passing argument 3 of ‘largestElements’ makes pointer from integer without a cast [enabled by default]
                     largestElements(array[ARRAY_SIZE], max[MAX_SIZE], rest[REST_SIZE]);
                     ^
    foo.c:8:6: note: expected ‘int *’ but argument is of type ‘int’
     void largestElements(int array[], int max[], int rest[]); 
          ^
    foo.c:14:48: warning: unused variable ‘a’ [-Wunused-variable]
         int option = 0, i = 0, n = 0, surplus = 0, a;
                                                    ^
    foo.c:14:35: warning: unused variable ‘surplus’ [-Wunused-variable]
         int option = 0, i = 0, n = 0, surplus = 0, a;
                                       ^
    foo.c:14:28: warning: unused variable ‘n’ [-Wunused-variable]
         int option = 0, i = 0, n = 0, surplus = 0, a;
    Line 7: You need to provide a type for a, so the compiler can check that you're using it correctly.
    Line 8 & 45: You're passing the arrays incorrectly. You don't need the [SIZE] part when you call the function. You just use the name of the array, like so: largestElements(array, max, rest);

    That second point is probably the source of your errors. Sorry, I don't have time to look at this in more detail, so there may be other problems or places for improvement.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 07-01-2014, 07:48 AM
  2. Replies: 9
    Last Post: 07-11-2013, 10:57 PM
  3. Replies: 16
    Last Post: 01-01-2008, 04:07 PM
  4. Passing pointers to arrays of char arrays
    By bobthebullet990 in forum C Programming
    Replies: 5
    Last Post: 03-31-2006, 05:31 AM
  5. Replies: 2
    Last Post: 02-23-2004, 06:34 AM

Tags for this Thread