Xxxxxx
Solved
Anonymous user
-
JooS Posted messages 2705 Status Membre -
JooS Posted messages 2705 Status Membre -
Hello,
We consider two arrays (tab_p and tab_i) of size 5. These arrays must contain integers.
- tab_p must contain even numbers.
- tab_i must contain odd numbers.
1\Create a program that fills the arrays as follows:
- An integer must be read from the keyboard
- If the value is even, fill tab_p
- Otherwise, if the value is odd, fill tab_i
2\Traverse each of these arrays using pointers to display their values as follows:
tab_even tab_i
. .
. .
. .
. .
Note: solve this exercise without using functions and ensure that only odd integers are entered when the array for even numbers is full.
PLEASE HELP ME AS SOON AS POSSIBLE to solve this exercise :(
Configuration: Windows 7 / Firefox 3.6.16
We consider two arrays (tab_p and tab_i) of size 5. These arrays must contain integers.
- tab_p must contain even numbers.
- tab_i must contain odd numbers.
1\Create a program that fills the arrays as follows:
- An integer must be read from the keyboard
- If the value is even, fill tab_p
- Otherwise, if the value is odd, fill tab_i
2\Traverse each of these arrays using pointers to display their values as follows:
tab_even tab_i
. .
. .
. .
. .
Note: solve this exercise without using functions and ensure that only odd integers are entered when the array for even numbers is full.
PLEASE HELP ME AS SOON AS POSSIBLE to solve this exercise :(
Configuration: Windows 7 / Firefox 3.6.16
3 réponses
Try this ...
PS: I couldn't try it since my compiler is bugging too much ...
#include <stdio.h> void main() { int tab_p[5], tab_i[5], p=0, i=0, nbr; tab_p[4] = NULL; tab_i[4] = NULL; while(i+p < 10) { if(!tab_p[4] && !tab_i[4]) printf("Give an integer: "); else if(!tab_p[4] && tab_i[4]) printf("Give an even number: "); else printf("Give an odd number: "); scanf("%d",&nbr); //---------------- if(nbr%2 == 0) { tab_p[p] = nbr; p++; } else { tab_i[i] = nbr; i++; } } scanf("\n"); } PS: I couldn't try it since my compiler is bugging too much ...
#include
#include
int main()
{int tab_p[5]; int tab_i[5]; int i=0; int val; int i_p=0,i_i=0;
while(i<10){
printf("enter an integer value: ");
scanf("%d",&val);
i++;
if(val%2==0){
tab_p[i_p]=val;
i++;}
else{
tab_i[i_i]=val;
i_i++;
}
start by indenting your code
shift your blocks one tab at a time, it will be much more readable
for your variables, no need to put int each time; declare your variable (initializing it if necessary) with a comma for the second variable... then a semicolon
if(val%2==0)
{
tab_p[i_p]=val;
i++;
}
else
{
tab_i[i_i]=val;
i_i++;
}
you mixed up your indices
if val is even, place it at index i_p of your even array and then increment i
otherwise, place it at index i_i of your odd array and then increment i_i
but you should increment i each time you fill a slot in your array
you should also increment i_p when you place a value in your even array
so you need to increment i outside of your condition, after your else
furthermore, once one of the arrays is full, you should add a loop for input to ensure that the entered value is correct; otherwise, you will have an error accessing your array
imagine you enter 5 even integers
your index i_p will therefore be at 4 (the last slot of your array), so if you mistakenly enter a 6th even integer, your code will try to access index 5 which does not exist and your program will crash
start by correcting how you increment your indices, check if it works while paying attention, and if it's good, add the proper loop when you ask to enter a value to be able, at the beginning, to enter odd or even numbers, then only even or only odd depending on which first array will be filled.
let us know if it works