Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Unexpected output in array

Unexpected output in array

Scheduled Pinned Locked Moved C / C++ / MFC
data-structures
5 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Member_15070286
    wrote on last edited by
    #1

    Hi All, //program to place even numbers in one array and odd numbers in another array PFA code. #include void main() { int array[20],array1[20],array2[20],i,n,j; printf("enter how many array elements\n"); scanf("%d",&n); for(i=0;i

    CPalliniC V 2 Replies Last reply
    0
    • M Member_15070286

      Hi All, //program to place even numbers in one array and odd numbers in another array PFA code. #include void main() { int array[20],array1[20],array2[20],i,n,j; printf("enter how many array elements\n"); scanf("%d",&n); for(i=0;i

      CPalliniC Offline
      CPalliniC Offline
      CPallini
      wrote on last edited by
      #2

      You are putting holes in your arrays of even and odd elements. In order to avoid that, you need to maintain two separate variables to keep track of how many even and odd items have been added so far. Try

      #include

      #define SIZE 20
      int main()
      {
      int items, odd_items, even_items;
      int array[SIZE], odd[SIZE], even[SIZE];
      printf("enter how many array elements\n");

      scanf("%d",&items);

      odd_items = even_items = 0;

      for (int n=0; n
      "In testa che avete, Signor di Ceprano?"
      -- Rigoletto

      In testa che avete, signor di Ceprano?

      Greg UtasG 1 Reply Last reply
      0
      • M Member_15070286

        Hi All, //program to place even numbers in one array and odd numbers in another array PFA code. #include void main() { int array[20],array1[20],array2[20],i,n,j; printf("enter how many array elements\n"); scanf("%d",&n); for(i=0;i

        V Offline
        V Offline
        Victor Nijegorodov
        wrote on last edited by
        #3

        Did you try

        scanf("%d",array[i]);

        without ampersand? Besides, main() is int, not void.

        1 Reply Last reply
        0
        • CPalliniC CPallini

          You are putting holes in your arrays of even and odd elements. In order to avoid that, you need to maintain two separate variables to keep track of how many even and odd items have been added so far. Try

          #include

          #define SIZE 20
          int main()
          {
          int items, odd_items, even_items;
          int array[SIZE], odd[SIZE], even[SIZE];
          printf("enter how many array elements\n");

          scanf("%d",&items);

          odd_items = even_items = 0;

          for (int n=0; n
          "In testa che avete, Signor di Ceprano?"
          -- Rigoletto

          Greg UtasG Offline
          Greg UtasG Offline
          Greg Utas
          wrote on last edited by
          #4

          5, but you should get 10 for looking at code formatted this way. :-D

          Robust Services Core | Software Techniques for Lemmings | Articles
          The fox knows many things, but the hedgehog knows one big thing.

          <p><a href="https://github.com/GregUtas/robust-services-core/blob/master/README.md">Robust Services Core</a>
          <em>The fox knows many things, but the hedgehog knows one big thing.</em></p>

          CPalliniC 1 Reply Last reply
          0
          • Greg UtasG Greg Utas

            5, but you should get 10 for looking at code formatted this way. :-D

            Robust Services Core | Software Techniques for Lemmings | Articles
            The fox knows many things, but the hedgehog knows one big thing.

            CPalliniC Offline
            CPalliniC Offline
            CPallini
            wrote on last edited by
            #5

            :-D Thank you.

            "In testa che avete, Signor di Ceprano?" -- Rigoletto

            In testa che avete, signor di Ceprano?

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups