C++ integer array question .. plz help

This is a discussion on C++ integer array question .. plz help within the C++ Programming forums, part of the General Programming Boards category; Question is: Declare two arrays of size 5. Get input from the user. Declare a third array of size 10. ...

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    10

    Angry C++ integer array question .. plz help

    Question is:

    Declare two arrays of size 5. Get input from the user. Declare a third array of size 10. Put the values of array1 in even indexes of array3 and values of array2 in odd indexes of array3.
    Example
    Array1
    1 2 3 4 5

    Array2
    6 7 8 9 10


    Array3 will be
    1 6 2 7 3 8 4 9 5 10


    im new in C++
    and i have getting a problem in this question..please check my code and help me to sort out the problem.

    Code:
    #include<iostream.h>
    #include<conio.h>
    #include<stdio.h>
    
    void main()
    
    {clrscr();
    
    int a[5];
    int b[5];
    int c[10];
    
    
    int s;
    
    for (s=0;s<5;s++)
    {cout<<"Enter Element of array 1";
    cin>>a[s];}
    
    
    for (s=0;s<5;s++)
    {
    cout<<"Enter element of array 2";
    cin>>b[s];
    }
    
    for (s=0;s<10;s++)
    {
    if (s%2==0)
    {c[s]=a[s];}
    
    else
    {c[s]=b[s];}
    
    }
    
    
    for (s=0;s<10;s++)
    {cout<<c[s]<<endl;}
    
    getch();
    
    }

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    1,625
    Maybe a better indentation would help you and us!
    Devoted my life to programming...

  3. #3
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    1,625
    C++ right? :

    Code:
    #include <iostream>
    #include <cconio>
    #include <cstdio>
    
    using namespace std;
    
    int main()
    {
        ...
        return 0;
    }
    Devoted my life to programming...

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    You can't be trying to get a[8] or b[9], as those things don't exist. Your index on a and b must never exceed 4.

  5. #5
    Registered User
    Join Date
    Oct 2010
    Posts
    10
    Quote Originally Posted by tabstop View Post
    You can't be trying to get a[8] or b[9], as those things don't exist. Your index on a and b must never exceed 4.
    thnks for ur hint .. i added a seperate counter for both arrays

    that is
    Code:
    if (s % 2 == 0)
        c[s] = a[q++];
    else
        c[s] = b[r++];
    it worked

  6. #6
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,625
    Rewrote to standard C++, fixed indentation, improved readability:
    Code:
    #include <iostream>
    #include <conio.h>
    #include <stdio.h>
    
    using namespace std;
    
    int main()
    {
    	int a[5];
    	int b[5];
    	int c[10];
    
    	for (int s = 0; s < 5; s++)
    	{
    		cout << "Enter Element of array 1";
    		cin >> a[s];
    	}
    
    
    	for (int s = 0; s < 5; s++)
    	{
    		cout << "Enter element of array 2";
    		cin >> b[s];
    	}
    
    	for (int s = 0; s < 10; s++)
    	{
    		if (s % 2 == 0)
    		{
    			c[s] = a[s];
    		}
    		else
    		{
    			c[s] = b[s];
    		}
    	}
    
    	for (int s = 0; s < 10; s++)
    	{
    		cout << c[s] << endl;
    	}
    }
    Did not fix bugs.
    Required reading:
    http://sourceforge.net/apps/mediawik...itle=Void_main
    Iostream.h is not a standard C++ header. All standard C++ headers have no extension: ie iostream.h -> iostream.
    Suggested reading:
    http://sourceforge.net/apps/mediawik...=Pause_console
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    zfk
    zfk is offline
    Registered User
    Join Date
    Dec 2010
    Location
    China
    Posts
    7
    Code:
    for (s=0;s<10;s++)
    {
    if (s%2==0)
    {c[s]=a[s];}
    
    else
    {c[s]=b[s];}
    
    }
    beyond array...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 08-16-2010, 10:00 AM
  2. Question on array
    By spurs01 in forum C Programming
    Replies: 2
    Last Post: 11-25-2009, 06:42 AM
  3. Newbie Question: sizeof() problem in function
    By Xeyide in forum C Programming
    Replies: 3
    Last Post: 09-04-2009, 12:05 AM
  4. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM
  5. Representing a Large Integer with an Array
    By random_accident in forum C Programming
    Replies: 3
    Last Post: 03-03-2005, 07:56 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21