Display ODD/EVEN nos. in a 2D ARRAY

Sep 21, 2013 at 7:04am
NOTE: has to use <stdio.h> and DO-WHILE format only IF POSSIBLE.

THIS is the 2D array I'm working on. I need it so that it will ask

"press 1 to show odd numbers"
"press 2 to show even numbers"

and it will display the proper numbers chosen by user!
I don't know how to combine these

i know that (array[x][y]%2==0) generates even numbers.
i know that (array[x][y]%2==0)
else printf("%d\t",ar[x][y]); displays odd numbers.

I however don't know how to take a user input and display the output of odd/even, or even how to combine it all in one program! :(

1
2
3
4
5
6
7
8
9
10
11
12
#include<stdio.h>
main(){
int x, y;
int array[5][4]={{4,-3,4,2},{-2,1,4,3},{5,3,6,-4},{3,2,7,5},{2,2,-9,6}};

x=0;
do{
y=0;
while(y<4){

printf("%d\t",array[x][y]);


i need to submit this in 7 HOURS, please reply ASAP.

IF anything, I rather just have the WORKING program codes please! I'll just dissect it and find out how you did it correctly so I can learn faster
Last edited on Sep 21, 2013 at 8:22am
Sep 21, 2013 at 11:31am
Hello, i have a question: are non-positive integers even or odd to your teacher?
Sep 21, 2013 at 12:29pm
including positive/non positive integers.
Sep 21, 2013 at 12:39pm
To be honest i don't remember so much about standard input output stdio.h -C language- but i can resolve it in C++ using iostream library -cout-

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
//TwoDimensionalArray.cpp
//Output data from two-dimensional array 

#include <iostream>
using std::cout;
using std::cin;
using std::endl;

void printArray(const int array[][4],const int r,const int c);
void printOddNumbers(const int array[][4],const int r,const int c);
void printEvenNumbers(const int array[][4],const int r,const int c);

int main(){

const int ROW=5;
const int COL=4;

int option;

int array[ROW][COL]={{4,-3,4,2},{-2,1,4,3},{5,3,6,-4},{3,2,7,5},{2,2,-9,6}};

do{

cout<<"\nI have a two-dimensional array of numbers"
	<<"\nPress 1 to show odd numbers"
	<<"\nPress 2 to show even numbers"
	<<"\nPress 3 to print the whole array"
	<<"\nPress 0 to quit."<<endl;
cin>>option;
while(option>3||option<0){
cout<<"\nPress 1 to show odd numbers"
        <<"\nPress 2 to show even numbers"
        <<"\nPress 3 to print the whole array"
        <<"\nPress 0 to quit."<<endl;
cin>>option;
}//end while

switch(option){
	case 1:
		cout<<endl;
		printOddNumbers(array,ROW,COL);
	break;
	case 2:
		cout<<endl;
		printEvenNumbers(array,ROW,COL);
	break;
	case 3:
		cout<<endl;
		printArray(array,ROW,COL);
	break;
	case 0:
	break;
	default:
	break;
		
}//end switch

}while(option!=0);
return 0; //indicates success
}//end main

void printArray(const int array[][4],const int r,const int c){
	for(int row=0;row<r;row++){
		for(int col=0;col<c;col++)
			cout<<array[row][col]<<' ';
		cout<<endl;
	}//end outer for

}//end function printArray

void printOddNumbers(const int array[][4],const int r,const int c){
	for(int row=0;row<r;row++){
                for(int col=0;col<c;col++){
                      	if(array[row][col]%2==0)
			continue;
			else
			  cout<<array[row][col]<<' ';
		}//end inner for
                cout<<endl;
        }//end outer for
}//end function printOddNumbers

void printEvenNumbers(const int array[][4],const int r,const int c){
        for(int row=0;row<r;row++){
                for(int col=0;col<c;col++){
                        if(array[row][col]%2==0)
                                cout<<array[row][col]<<' ';
                        else
                         continue;
                }//end inner for
                cout<<endl;
        }//end outer for
}//end function printEvenNumbers 



I have a two-dimensional array of numbers
Press 1 to show odd numbers
Press 2 to show even numbers
Press 3 to print the whole array
Press 0 to quit.
1

-3 
1 3 
5 3 
3 7 5 
-9 

I have a two-dimensional array of numbers
Press 1 to show odd numbers
Press 2 to show even numbers
Press 3 to print the whole array
Press 0 to quit.
2

4 4 2 
-2 4 
6 -4 
2 
2 2 6 

I have a two-dimensional array of numbers
Press 1 to show odd numbers
Press 2 to show even numbers
Press 3 to print the whole array
Press 0 to quit.
3

4 -3 4 2 
-2 1 4 3 
5 3 6 -4 
3 2 7 5 
2 2 -9 6 

I have a two-dimensional array of numbers
Press 1 to show odd numbers
Press 2 to show even numbers
Press 3 to print the whole array
Press 0 to quit.
0
Sep 21, 2013 at 12:48pm
theres a problem with your correct program enrique, I need it to run on <stdio.h>, without using cout & sin.

i need it to be in a do while if else, else if format, or else the solution is not valid sad to say

we haven't brushed on this form of programming yet I'm sorry, but the professor wont take this.

this is what I've done so far but im doing something wrong and it wont run on .C format and saying something is wrong with the input codes.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45

#include<stdio.h>
main(){
int x, y;
int ar[5][4]={{4,-3,4,2},{-2,1,4,3},{5,3,6,-4},{3,2,7,5},{2,2,-9,6}};

x=0;
do{
  y=0;
  while(y<4){   
   //Display all nos. inside the array
   printf("%d\t",ar[x][y]);  
   
   //Display all EVEN nos. inside the array
   /*if(ar[x][y]%2==0)
       printf("%d\t",ar[x][y]);/*
   
   //Display all ODD nos. inside the array
   /*if(ar[x][y]%2!=0)
       else printf("%d\t",ar[x][y]);*/
   
   y++;        
  }  
printf("\n");       
x++;    
}while(x<5);  

 
printf ("\n Press 1 to show ODD numbers only.");  
printf ("\n Press 2 to show EVEN numbers only.");

if (input=1) {
         if(ar[x][y]%2!=0);
         printf("%d\t",ar[x][y]);
         }

if (input=2) {
         if (ar[x][y]%2==0);
         printf("%d\t",ar[x][y])
         }
else printf ("Invalid Number. Please only select 1 or 2.")
                  
getch();       
}


feel free to rearrange it the way it should look, that would make it run on .C
Last edited on Sep 21, 2013 at 12:49pm
Sep 21, 2013 at 12:58pm
I REALLY need this done ASAP in 2 HOURS! please someone help me.
Sep 21, 2013 at 1:00pm
after recalling C, and debugging for 1 hour, here take a look:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <stdio.h>

int main()
{
	int x;
	
	/* 4rows and 5columns */
	int array[4][5]={ 4, -3, 4,  2, -2,
					  1,  4, 3,  5,  3,
					  6, -4, 3,  2,  7,
					  5,  2, 2, -9,  6 };

	printf ( "\nPress 1 to show odd numbers\n" );
	printf ( "Press 2 to show even numbers\n" );
	scanf ( "%i", &x );
	if ( x == 1 ) {
			/* scan each values in the array */
			int i;
			int j;
			for ( i = 0; i < 4; i++ ) {
				for ( j = 0; j < 5; j++ ) {
					if ( array[i][j] % 2 == 1 || array[i][j] % 2 == -1 )
						printf ( "%i\n", array[i][j] );
				}
			}
		
	} else if ( x == 2 ) {
			int k;
			int m;
			for ( k = 0; k < 4; k++ ) {
				for ( m = 0; m < 5; m++ ) {
					if ( array[k][m] % 2 == 0 )
						printf ( "%i\n", array[k][m] );
				}
			}
		
	} else
		printf ( "\nNext time enter a valid option!\n" );
	
	
	return 0;
}


if you want 5 row by 4 column then hopefully you can change some of this code by looking at the example
Last edited on Sep 21, 2013 at 1:06pm
Sep 21, 2013 at 1:00pm
Try to implement this C++ solution in C using scanf + printf + if-else + do-while, will be a good exercise for you!

for example:
1.-you can replace cout statement for printf
2.-you can keep using a while loop inside the do-while loop to validate input
3.-replace the switch statement for nested if-else combinations
4.-You can use the function implementations from printArray,printEvenNumbers
printOddNumbers inside the outer do-while loop

That's all that i can do for you, share this solution with your partners and resolve it in group.

Best regards.
Last edited on Sep 21, 2013 at 1:01pm
Sep 21, 2013 at 1:16pm
nevermore28

when i run your program, it lets me press a button but when i enter 1 or 2 it closes, whats wrong?

also needs to be in a do while format

eyenrique,

I'm doing this by myself, you can say the teacher lacks the love for his students to give a simple programming assignment for a typical college marketing student.
Sep 21, 2013 at 1:19pm
that is because it has no some sort of "pause" function at the end:

this should work with no errors:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <stdio.h>

int main()
{
	int x;
	
	/* 4rows and 5columns */
	int array[4][5]={ 4, -3, 4,  2, -2,
					1,  4, 3,  5,  3,
					6, -4, 3,  2,  7,
					5,  2, 2, -9,  6 };

	printf ( "\nPress 1 to show odd numbers\n" );
	printf ( "Press 2 to show even numbers\n" );
	scanf ( "%i", &x );
	if ( x == 1 ) {
			/* scan each values in the array */
			int i = 0;
			int j = 0;
			do {
				do {
					if ( array[i][j] % 2 == 1 || array[i][j] % 2 == -1 )
						printf ( "%i\n", array[i][j] );
					j++;
				} while ( j < 5 );
				j = 0;
				i++;
			} while ( i < 4 );
				
	} else if ( x == 2 ) {
			int k = 0;
			int m = 0;
			do {
				do {
					if ( array[k][m] % 2 == 0 )
						printf ( "%i\n", array[k][m] );
					m++;
				} while ( m < 5 );
				m = 0;
				k++;
			} while ( k < 4 );
		
	} else
		printf ( "\nNext time enter a valid option!\n" );
	
	
	getch();
	return 0;
}
Sep 21, 2013 at 1:24pm
You don't have to learn a low-level language ( C ) for a college of marketing.
There are more friendly languages -C#,BASIC,Scheme,Java..-
Well i'll try to implement this in C but i can't promise anything.
Sep 21, 2013 at 1:38pm
thanks for all the help enrique and nevermore!

heres basically what i want the program to look like:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37

#include<stdio.h>
main(){
int x, y;
int ar[5][4]={{4,-3,4,2},{-2,1,4,3},{5,3,6,-4},{3,2,7,5},{2,2,-9,6}};

x=0;
do{
  y=0;
  while(y<4){   
   printf("%d\t",ar[x][y]);  
   y++;        
  }  
printf("\n");       
x++;    
}while(x<5);  

 
printf ("\n Press 1 to show ODD numbers only.");  
printf ("\n Press 2 to show EVEN numbers only.\n\n");

scanf ( "%i", &x );      
if (x==1) {     
         if(ar[x][y]%2!=0);
         printf("%d\t",ar[x][y]);
         }
if (x==2) {
         if (ar[x][y]%2==0);
         printf("%d\t",ar[x][y]);
         }
if (x<1||x>2) {
     printf("\n You didn't press 1 or 2. Please try again.");
                 
}                  
getch();       
}


it displays the wrong odd/even answer that's what I don't get. I could've sworn i was doing something right though... but basically this is the idea.
Sep 21, 2013 at 1:50pm
try:

1
2
3
4
5
6
7
8
9
10
11
12
13
if (x==1) {     
    if(ar[x][y]%2!=0);
		printf("%d\t",ar[x][y]);
}
else (x==2) {
    if (ar[x][y]%2==0);
		printf("%d\t",ar[x][y]);
}
else
     printf("\n You didn't press 1 or 2. Please try again.");
              
getch();       
}
Sep 21, 2013 at 1:53pm
syntax error before else.

also when i try my code, when i type 1 it only displays 5, at 2 it displays 3.

something is defenitely wrong, and i cant put my finger on it.

its doing something its not supposed to do, or doing it incorrectly
Last edited on Sep 21, 2013 at 1:54pm
Sep 21, 2013 at 1:56pm
Well there is -format isnt so good but works-

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
//TDarray.c

#include <stdio.h>

int main(){

int option;

int array[5][4]={{4,-3,4,2},{-2,1,4,3},{5,3,6,-4},{3,2,7,5},{2,2,-9,6}};

do{

printf("\nI have a two-dimensional array of numbers");
printf("\nPress 1 to show odd numbers");
printf("\nPress 2 to show even numbers");
printf("\nPress 3 to print the whole array");
printf("\nPress 0 to quit.");
scanf("%d",&option);
while(option>3||option<0){
printf("\nPress 1 to show odd numbers");
printf("\nPress 2 to show even numbers");
printf("\nPress 3 to print the whole array");
printf("\nPress 0 to quit.");
scanf("%d",&option);
}//end while

	

if(option==1){
	int row;
	int col;
	for(row=0;row<5;row++){
                for(col=0;col<4;col++){
                      	if(array[row][col]%2==0)
			continue;
			else{
			  printf("%i",array[row][col]);
			printf(" ");
			}//end if..else	
		}//end inner for
                printf("\n");
        }//end outer for
}else if(option==2){
	int row;
	int col;
	for(row=0;row<5;row++){
                for(col=0;col<4;col++){
                        if(array[row][col]%2==0)
                                printf("%i",array[row][col]);
                        else
                         continue;
                }//end inner for
                printf(" ");
        }//end outer for(
}else if(option==3){
	int row;
	int col;
	for(row=0;row<5;row++){
		for(col=0;col<4;col++)
			printf("%i",array[row][col]);
		printf(" ");
	}//end outer for
}else{
	break;
}//end nested if..else

}while(option!=0);

return 0; //indicates success
}//end main
Sep 21, 2013 at 2:00pm
that is because you didn't put that on any loop:

as i said earlier:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include <stdio.h>

int main()
{
	int x;
	
	/* 4rows and 5columns */
	int array[4][5]={ 4, -3, 4,  2, -2,
					1,  4, 3,  5,  3,
					6, -4, 3,  2,  7,
					5,  2, 2, -9,  6 };

	printf ( "\nPress 1 to show odd numbers\n" );
	printf ( "Press 2 to show even numbers\n" );
	scanf ( "%i", &x );
	if ( x == 1 ) {
			/* scan each values in the array */
			int i = 0;
			int j = 0;
			do {
				do {
					if ( array[i][j] % 2 == 1 || array[i][j] % 2 == -1 )
						printf ( "%i\n", array[i][j] );
					j++;
				} while ( j < 5 );
				j = 0;
				i++;
			} while ( i < 4 );
				
	} else if ( x == 2 ) {
			int k = 0;
			int m = 0;
			do {
				do {
					if ( array[k][m] % 2 == 0 )
						printf ( "%i\n", array[k][m] );
					m++;
				} while ( m < 5 );
				m = 0;
				k++;
			} while ( k < 4 );
		
	} else
		printf ( "\nNext time enter a valid option!\n" );
	
	
	getch();
	return 0;
}
Sep 21, 2013 at 2:04pm
-format fixed-

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
//TDarray.c

#include <stdio.h>

int main(){

int option;

int array[5][4]={{4,-3,4,2},{-2,1,4,3},{5,3,6,-4},{3,2,7,5},{2,2,-9,6}};

do{

printf("\nI have a two-dimensional array of numbers");
printf("\nPress 1 to show odd numbers");
printf("\nPress 2 to show even numbers");
printf("\nPress 3 to print the whole array");
printf("\nPress 0 to quit.");
scanf("%d",&option);
while(option>3||option<0){
printf("\nPress 1 to show odd numbers");
printf("\nPress 2 to show even numbers");
printf("\nPress 3 to print the whole array");
printf("\nPress 0 to quit.");
scanf("%d",&option);
}//end while

	

if(option==1){
	int row;
	int col;
	for(row=0;row<5;row++){
                for(col=0;col<4;col++){
                      	if(array[row][col]%2==0)
			continue;
			else{
			  printf("%i",array[row][col]);
			printf(" ");
			}//end if..else	
		}//end inner for
                printf("\n");
        }//end outer for
}else if(option==2){
	int row;
	int col;
	for(row=0;row<5;row++){
                for(col=0;col<4;col++){
                        if(array[row][col]%2==0)
                                printf("%i",array[row][col]);
                        else
                         continue;
                }//end inner for
                printf("\n");
        }//end outer for(
}else if(option==3){
	int row;
	int col;
	for(row=0;row<5;row++){
		for(col=0;col<4;col++)
			printf("%i",array[row][col]);
		printf("\n");
	}//end outer for
}else{
	break;
}//end nested if..else

}while(option!=0);

return 0; //indicates success
}//end main



I have a two-dimensional array of numbers
Press 1 to show odd numbers
Press 2 to show even numbers
Press 3 to print the whole array
Press 0 to quit.1
-3 
1 3 
5 3 
3 7 5 
-9 

I have a two-dimensional array of numbers
Press 1 to show odd numbers
Press 2 to show even numbers
Press 3 to print the whole array
Press 0 to quit.2
442
-24
6-4
2
226

I have a two-dimensional array of numbers
Press 1 to show odd numbers
Press 2 to show even numbers
Press 3 to print the whole array
Press 0 to quit.3
4-342
-2143
536-4
3275
22-96

I have a two-dimensional array of numbers
Press 1 to show odd numbers
Press 2 to show even numbers
Press 3 to print the whole array
Press 0 to quit.0
Sep 21, 2013 at 2:04pm
OMG thank you so much! I guess I wont be failing programming this year!

I'll be back sometime tomorrow or day after to add my final programming exercise if I cant figure it out by myself.

YOU guys deserve a great pat on the back, THANKS and CHEERS!
Sep 21, 2013 at 2:10pm
Well i can do final exercises for a few dollars xD
hahaha just kidding but isn't a bad idea in desperate cases.
Topic archived. No new replies allowed.