1 Replies - 188 Views - Last Post: 20 April 2013 - 11:46 AM Rate Topic: -----

#1 mohamed_200259  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 19-April 13

why w use counter in this code and use for loop before display items

Posted 20 April 2013 - 10:49 AM

#include<iostream>
using namespace std;
int main()
{
    int A[5],B[5],C[5],b=0,c=0;
    cout<<"enter ur array please\n";
    for(int a=0;a<5;a++)
        cin>>A[a];
    for(int a=0;a<5;a++)
        if(A[a]%2==0)
    {
        B[b]=A[a];
        b++;
    }
    else
    {
        C[c]=A[a];
        c++;
    }
    cout<<"even items in array is\n";
    for(int a=0;a<b;a++)
        cout<<B[a]<<"\t";
    cout<<"\nodd items in array is\n";
    for(int a=0;a<c;a++)
    cout<<C[a]<<"\t";
      return 0;
}

This post has been edited by jimblumberg: 20 April 2013 - 07:20 PM
Reason for edit:: Added missing code tags. Please learn to use them properly.


Is This A Good Question/Topic? 0
  • +

Replies To: why w use counter in this code and use for loop before display items

#2 David W  Icon User is offline

  • DIC supporter
  • member icon

Reputation: 281
  • View blog
  • Posts: 1,788
  • Joined: 20-September 08

Re: why w use counter in this code and use for loop before display items

Posted 20 April 2013 - 11:46 AM

When submitting code, please wrap your program inside [ code ] ... [ /code ] tags

And spacing things out some can help too.

Also ... you had a redundant loop ... see fixed version below:

include <iostream>

using namespace std;

int main()
{
    int A[5], B[5], C[5], b = 0, c = 0;

    cout << "Enter array 'a' please: \n";
    for( int a = 0; a < 5 ; a++ )
    {
        cout << "Enter element " << a+1 << ": " << flush;
        cin >> A[a];

        if( A[a] % 2 == 0 )
        {
            B[b] = A[a];
            b++;
        }
        else
        {
            C[c] = A[a];
            c++;
        }
    }

    cout << "Even items in array are:\n";
    for( int  a = 0; a < b; a++ )
        cout << B[a] << "\t";

    cout << "\nOdd items in array are:\n";
    for( int a = 0; a < c; a++ )
        cout << C[a] << "\t";
    
    cout << flush;
    return 0;
}


This post has been edited by David W: 20 April 2013 - 12:46 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1