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?