| | |
Array Question
![]() |
I have an Array A[N]={1,2,3,4,5,6};
and I want to split it to 2 arrays of size N/2
such that B = 1, 2, 3 and C = 4, 5, 6
here is my program but there is something wrong with array C
Can you help me with it ?
and I want to split it to 2 arrays of size N/2
such that B = 1, 2, 3 and C = 4, 5, 6
here is my program but there is something wrong with array C
Can you help me with it ?
C++ Syntax (Toggle Plain Text)
#include <iostream.h> void print(int X[], int newsize); void main () { int const N=6; int A[N]={1,2,3,4,5,6}; int C[N/2],B[N/2]; for(int i =0;i<N;i++) { if(i>N/2) { int k=0; C[k]=A[i]; k++; } else B[i]=A[i]; } print(B,N); cout<<endl; print(C,N); } void print(int X[],int size) { int newsize; newsize=size/2; for(int i=0;i<newsize;i++) cout<<X[i]<<"\t"; }
•
•
Join Date: Jun 2008
Posts: 182
Reputation:
Solved Threads: 18
Some general considerations first:
1 - main must return an int, so it's
2 - you use deprecated
Coming to the "real" errors:
1 - instead of
2 - You initialize k to 0 every time you enter the if block, basically rewriting only the first position of the C array. Take k declaration out of the loop, like this:
1 - main must return an int, so it's
int main() and not void main() . I think your compiler should have issued a warning at least. Here's an explanation of why.2 - you use deprecated
<iostream.h> instead of <iostream> . To this also your compiler should have issued a warning. Take a look at this before changing it.Coming to the "real" errors:
1 - instead of
if(i>N/2) it should be if(i>=N/2) and2 - You initialize k to 0 every time you enter the if block, basically rewriting only the first position of the C array. Take k declaration out of the loop, like this:
C++ Syntax (Toggle Plain Text)
int k=0; for(int i =0;i<N;i++) { if(i>=N/2) { C[k]=A[i]; k++; } else B[i]=A[i]; }
Last edited by mrboolf; Dec 3rd, 2008 at 12:45 pm.
![]() |
Similar Threads
- C++ Array Question (C++)
- 2 D array question (C++)
- Strings in array [I NEED HELP] C lang. (C)
- Class with dynamic array how? (C++)
- File array question (C++)
- Array question (C++)
- array question (C++)
Other Threads in the C++ Forum
- Previous Thread: Average Program using an Array
- Next Thread: I need to help
| Thread Tools | Search this Thread |
action api array auto based beginner binary bitmap c++ c/c++ calculator challenge char class classes code coding compile console conversion count createcopyofanyfileinc delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game garbage givemetehcodez graph gui hmenu homeworkhelp homeworkhelper iamthwee ifstream input insert int integer java lib linkedlist linker loop looping loops map math matrix memory multiple news node noob output parameter pointer primenumbersinrange problem program programming project python random read recursion reference rpg sockets string strings temperature template test text text-file tree url variable vector video win32 windows winsock wordfrequency wxwidgets






