0

I'm trying to make a program allowing a user to input positive integers into a one-dimensional array size 12 and trap inputs less than 1. The program then copies integers with a value less than 30 into a second three-dimensional array. And it also copies the integers greater than or equal to 30 into a third three-dimensional array.

It will display the three arrays together in a tabular form. The program will use three arrays, 1 single array, and 2 three-dimensional arrays.

This is what I did:

#include <stdio.h>

#define N 12


int main() {

    int a[N], b[2][2][3], c[2][2][3];
    int i, j, k, l;

        for(i = 0; i < N; i++){
            printf("Input %d: ", i);
            scanf("%d", &a[i]);

            if(a[i] < 30){
                for(j = 0; j < 2; j++){
                    for(k = 0; k < 2; k++){
                        for(l = 0; l < 3; l++){
                            b[j][k][l] = a[i];
                        }
                    }
                }
            }

            if(a[i] >= 30){
                for(j = 0; j < 2; j++){
                    for(k = 0; k < 2; k++){
                        for(l = 0; l < 3; l++){
                            c[j][k][l] = a[i];
                        }
                    }
                }
            }

            while(a[i] < 1){
                if (a[i] < 5){
                    printf("\nInvalid Input\nEnter 1 or greater than 1 only\n\n");
                    printf("Input %d: ", i);
                    scanf("%d", &a[i]);

                    if(a[i] < 30){
                        for(j = 0; j < 2; j++){
                            for(k = 0; k < 2; k++){
                                for(l = 0; l < 3; l++){
                                    b[j][k][l] = a[i];
                                }
                            }
                        }
                    }

                    if(a[i] >= 30){
                        for(j = 0; j < 2; j++){
                            for(k = 0; k < 2; k++){
                                for(l = 0; l < 3; l++){
                                    c[j][k][l] = a[i];
                                }
                            }
                        }
                    }
                }
            }

        }

        printf("\nArray 1: \n");

        for(i = 0; i < N; i++)
            printf("a[%d] = %d \n", i, a[i]);

        printf("\n\nArray with less than 30: \n");

        for(j = 0; j < 2; j++){
                    for(k = 0; k < 2; k++){
                        for(l = 0; l < 3; l++){
                            printf("b[%d][%d][%d] = %d \n", j, k, l, b[j][k][l]); 
                        }
                    }
                }

        printf("\n\nArray with greater than or equal to 30: \n");

        for(j = 0; j < 2; j++){
                    for(k = 0; k < 2; k++){
                        for(l = 0; l < 3; l++){
                            printf("c[%d][%d][%d] = %d \n", j, k, l, c[j][k][l]); 
                        }
                    }
                }
}

However when I try to run the program it displays the wrong values. Could someone please enlighten me what's wrong with my code? Thank you.

Edit: I have changed the indexing to start with 0 not 1.

When I try to input this:

Input 0: 1
Input 1: 2
Input 2: 3
Input 3: 4
Input 4: 5
Input 5: 6
Input 6: 31
Input 7: 32
Input 8: 33
Input 9: 34
Input 10: 35
Input 11: 36

It shows the result I want in array 1:

Array 1:
a[0] = 1
a[1] = 2
a[2] = 3
a[3] = 4
a[4] = 5
a[5] = 6
a[6] = 31
a[7] = 32
a[8] = 33
a[9] = 34
a[10] = 35
a[11] = 36

However, in both the three-dimensional arrays, it only displays the last integer that I have inputted that fits the condition:

Array with less than 30:
b[0][0][0] = 6
b[0][0][1] = 6
b[0][0][2] = 6
b[0][1][0] = 6
b[0][1][1] = 6
b[0][1][2] = 6
b[1][0][0] = 6
b[1][0][1] = 6
b[1][0][2] = 6
b[1][1][0] = 6
b[1][1][1] = 6
b[1][1][2] = 6


Array with greater than or equal to 30:
c[0][0][0] = 36
c[0][0][1] = 36
c[0][0][2] = 36
c[0][1][0] = 36
c[0][1][1] = 36
c[0][1][2] = 36
c[1][0][0] = 36
c[1][0][1] = 36
c[1][0][2] = 36
c[1][1][0] = 36
c[1][1][1] = 36
c[1][1][2] = 36

What I'm trying to get is that in the three-dimensional arrays "Array with less than 30:" displays ALL less than 30

And also in "Array with greater than or equal to 30:" displays ALL greater than or equal to 30.

Like this:

Array with less than 30:
b[0][0][0] = 1
b[0][0][1] = 2
b[0][0][2] = 3
b[0][1][0] = 4
b[0][1][1] = 5
b[0][1][2] = 6
b[1][0][0] = 0
b[1][0][1] = 0
b[1][0][2] = 0
b[1][1][0] = 0
b[1][1][1] = 0
b[1][1][2] = 0


Array with greater than or equal to 30:
c[0][0][0] = 31
c[0][0][1] = 32
c[0][0][2] = 33
c[0][1][0] = 34
c[0][1][1] = 35
c[0][1][2] = 36
c[1][0][0] = 0
c[1][0][1] = 0
c[1][0][2] = 0
c[1][1][0] = 0
c[1][1][1] = 0
c[1][1][2] = 0

Would it be possible to do this? Or are there any alternatives that are similar?

  • 1
    In C, array indexing starts with 0, not 1... Fix those... Eg: `for( j = 0; j < 2; j++ ) {` (This must be a duplicate...) – Fe2O3 Mar 06 '23 at 04:44
  • Follow on question: Why do you have so much code that repeats the same mistake? Did you stop to test any of the functions before copy/paste to create so many lines of bad code? A lesson to learn is "test, Test, TEST!" Don't write novels, write programs... – Fe2O3 Mar 06 '23 at 04:56
  • Have you tried running your code line-by-line in a debugger while monitoring the control flow and the values of all variables, in order to determine in which line your program stops behaving as intended? If you did not try this, then you may want to read this: [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/q/25385173/12149471) You may also want to read this: [How to debug small programs?](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) – Andreas Wenzel Mar 06 '23 at 05:01
  • I'm still a bit lost at what you're trying to do here. Assuming your indexing gets fixed, you're filling the entire 3D array with whatever is in `a[i]`. If that's your intent, might as well use [`memset`](https://linux.die.net/man/3/memset) in a one-liner instead of rolling your own 3-nested-`for`-loops each time. – yano Mar 06 '23 at 05:02
  • Sorry about that. Just updated my post. Feel free to add any insights. @Fe2O3 – TheThirdReign Mar 06 '23 at 07:23
  • @yano What I'm trying to get is that in the three-dimensional arrays "Array with less than 30:" displays ALL less than 30. And also in "Array with greater than or equal to 30:" displays ALL greater than or equal to 30. We aren't allowed to use any other methods including memset. Would it be possible? – TheThirdReign Mar 06 '23 at 07:26
  • ok, I see now that setting the entire 3D array to one value was _not_ your intent, so using `memset` no longer makes sense. – yano Mar 06 '23 at 15:30

1 Answers1

2

The following might show how to use less code to achieve your objective. Please note that the printing of b[] and c[] could be done in a function called twice (instead of duplicated code.)

#include <stdio.h>

#define N 12

int main( void ) {
    int a[N] = { 0 } // initialise arrays on the stack
    int b[2][2][3] = { 0 };
    int c[2][2][3] = { 0 };
    int i, j, k, l;

    for( i = 0; i < N; i++ ) {
        // localise validation of user input
        do {
            printf("Input %d: ", i);
#if 0
            if( scanf( "%d", &a[i] ) != 1 || a[i] < 1 )
                puts( "Bad input. Repeat..." );
#else // correction to clear the input buffer if user enters "foobar"
            if( scanf( "%d", &a[i] ) != 1 || a[i] < 1 ) {
                scanf( "%*[^\n]" );
                puts( "Bad input. Repeat..." );
            }
#endif
        } while( a[i] < 1 );

        int done = 0; // a flag (to break out of nested loops.
        for(j = 0; j < 2 && !done; j++)
            for(k = 0; k < 2 && !done; k++)
                for(l = 0; l < 3 && !done; l++)
                    if( a[i] < 30 ) {
                        if( b[j][k][l] == 0 ) { // only replace 0's
                            b[j][k][l] = a[i];
                            done = 1;
                        }
                    } else {
                        if( c[j][k][l] == 0 ) {
                            c[j][k][l] = a[i];
                            done = 1;
                        }
                    }
    }
    
    /* then do the printing stuff */
}

Don't use copy/paste to write code. And, don't write more than a few lines of code before stopping to compile and test what you've written/changed. "Incremental development".


EDIT:
Having pressured the OP to write less code that does the job, it seems appropriate to improve on the offering above. This still uses the 3D array for storage.

    int *store = NULL;
    for(j = 0; j < 2 && !store; j++)
        for(k = 0; k < 2 && !store; k++)
            for(l = 0; l < 3 && !store; l++)
                if( a[i] < 30 ) {
                    if( b[j][k][l] == 0 )
                        store = &b[j][k][l];
                } else {
                    if( c[j][k][l] == 0 ) {
                        store = &c[j][k][l];
                }
    *store = a[i];
Fe2O3
  • 6,077
  • 2
  • 4
  • 20
  • 1
    Thank you! This is exactly what I'm trying to get. I'll try to write more efficiently next time. I'm still new to programming so I really appreciate the advice. – TheThirdReign Mar 06 '23 at 08:06
  • "Shun advice/At any price/That's what I call/Good advice!" `:-)`... Just posted correction. Enjoy – Fe2O3 Mar 06 '23 at 08:07