Xxxxxx

Solved
Anonymous user -  
JooS Posted messages 2705 Status Member -
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

3 answers

  1. JooS Posted messages 2705 Status Member 228
     
    Hi, what have you been able to do so far?
    0
    1. Anonymous user
       
      slt here is the code I started for now


      #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++;
      }
      0
    2. oliver3923 Posted messages 1059 Status Member 143
       
      salut

      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.
      0
    3. Anonymous user
       
      but how did I not see your explanation from the beginning; it's unbelievable!!!! OLIVER you saw right away that I'm a beginner in C actually I just started it not long ago but you've helped me a lot here THANK YOU!!!!!!!!!!
      0
    4. oliver3923 Posted messages 1059 Status Member 143
       
      it's okay don't worry

      let us know if it works
      0
    5. Anonymous user
       
      Ok, I will make sure to inform you, but I need to install Borland C++ because it doesn't work on Dev-C++.
      0
  2. JooS Posted messages 2705 Status Member 228
     
    Try this ...

    #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 ...
    0
  3. Anonymous user
     
    I tried, but it still doesn't work. I'll try to find the mistake. You're really kind; you’re an angel!!!!!!!
    0
    1. JooS Posted messages 2705 Status Member 228
       
      What compiler do you have, because most display the line where the error is!!!
      0
    2. Anonymous user
       
      The compiler stops here: tab_p[4] = NULL; and I don't know why?
      0
    3. Anonymous user
       
      I have DEV-C++.
      0
    4. oliver3923 Posted messages 1059 Status Member 143
       
      I'm not sure we can declare an integer as null

      remove these two lines to see what happens

      also, you do it twice^^ (the name of the second one needs to be changed)
      0
    5. JooS Posted messages 2705 Status Member 228
       
      oops, the second one is for tab_i
      0