Whats wrong with this?

This is a discussion on Whats wrong with this? within the C++ Programming forums, part of the General Programming Boards category; Code: // This is the main project file for VC++ application project // generated using an Application Wizard. #include "stdafx.h" ...

  1. #1
    Registered User
    Join Date
    Apr 2005
    Posts
    41

    Whats wrong with this?

    Code:
    // This is the main project file for VC++ application project 
    // generated using an Application Wizard.
    
    #include "stdafx.h"
    
    #using <mscorlib.dll>
    
    using namespace System;
    void printA(Int32 []); //Passing Array To a Funciton
    void populate(Int32[],int,int);
    void split(Int32 [],Int32 [],Int32 []);
    
    
    int _tmain()
    {
    	
     
    	int n;
    	
    	Console::Write(S"How Many Elements wou you like? ");
    	n=Int32::Parse(Console::ReadLine());
    	
    	Int32 y[]= new Int32[n];
    	Int32 x[]= new Int32[n/2];
    	Int32 z[]= new Int32[n/2];
    
    	
    	int a =1, b=11;
    	populate(y,a,b);
    	printA(y);
    	split(y,x,z);
    
    	
    
    	return 0;
    }
    
    void printA(Int32 b[]){
    
    for(int i=0; i<b->Length; i++)
    Console::WriteLine(S"{0}",
    		b[i].ToString());
    }
    
    
    void populate(Int32 a[], int low, int high) {
    
    	Random *r;
    			r= new Random();	
    	for(int i=0; i<a->Length; i++)
    		a[i] =r->Next(low,high);
     
    }
    
    
    void split(Int32  a[], Int32 a1[], Int32 a2[]) {
    
    	int mid = (a->Length)/2;
    	
    
    	for (int i=0; i!=mid; i++){
    		      a1[i]=a[i];
    		for(int j =mid; j!=a->Length; j++)
    		       a2[i]= a[j];
    	}
    
    	Console::WriteLine(S"SubArray1: ");
    	printA(a1);
    	Console::WriteLine(S"SubArray2: ");
    	printA(a2);
    }
    I am trying to split an array in half and store each halve in a separate subarray. The first subarray outputs fine, the second subarray skips the element a[n-1] and instead outputs a[n] twice. Its probably a simple fix that I am not seeing right now. Thanks for the help.
    Last edited by blindman858; 05-13-2005 at 11:22 PM.

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    > Int32 z[]= new Int32[n/2];
    This should probably be:
    Int32 z[]= new Int32[n/2+1];
    Because if your original array is 9 elements, your second subarray will contain 5 elements.
    Code:
    >	for (int i=0; i!=mid; i++){
    >		      a1[i]=a[i];
    >		for(int j =mid; j!=a->Length; j++)
    >		       a2[i]= a[j];
    >	}
    I think you want two separate for-loops:
    Code:
    	for (int i=0; i!=mid; i++){
    		      a1[i]=a[i];
    	}
    		for(int i=0,int j =mid; j!=a->Length; i++,j++){
    		       a2[i]= a[j];
    	}

  3. #3
    Registered User
    Join Date
    Apr 2005
    Posts
    41
    NVM I found my own mistake. For those that would like to know what was wrong here is the modified split function:

    Code:
    void split(Int32  a[], Int32 a1[], Int32 a2[]) {
    
    	int mid = (a->Length)/2;
    	
    
    	for (int i=0; i!=mid; i++)
    			a1[i]=a[i];
    	
    	for( int j = 0 ; (j+mid)!=a->Length; j++)
    		a2[j]= a[j+mid];
    	
    	
    
    	Console::WriteLine(S"SubArray1: ");
    	printA(a1);
    	Console::WriteLine(S"SubArray2: ");
    	printA(a2);
    }
    I had one for loop inside the other which was wrong.

  4. #4
    Registered User
    Join Date
    Apr 2005
    Posts
    41
    hehe u beatme to it swoopy. Yeah I made it so that it only does an even array because I am ultimately trying to code the merge sort algorithm.

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >hehe u beatme to it swoopy.
    By mere seconds.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 07-15-2004, 04:30 PM
  2. Debugging-Looking in the wrong places
    By JaWiB in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 11-03-2003, 10:50 PM
  3. Confused: What is wrong with void??
    By Machewy in forum C++ Programming
    Replies: 19
    Last Post: 04-15-2003, 01:40 PM
  4. God
    By datainjector in forum A Brief History of Cprogramming.com
    Replies: 746
    Last Post: 12-22-2002, 12:01 PM
  5. Whats wrong?
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 07-14-2002, 02:04 PM

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