SRL Archive - C/C++ Help and Tutorials

Help with arrays


Overtime

2011-11-02 17:09:27

so i have to create this function using arrays


//
// Split the elements of a source array into two destination arrays
// The given size is the size of the destination arrays (they are the same size)
//
// WRITE CODE TO COMPLETE THIS FUNCTION
//

void splitArray(int dest_one[], int dest_two[], int source[], int size)
{
int ctr = 0; // Counter that will go through the source[] array.
int i; // A counter



// Loop through the destination arrays.
// Assign each element in a destination array the next element
// in the source array. Use the ctr variable to keep track of the
// current position in the source array.




}

Sabzi

2011-11-02 17:38:42

for(i = 0;i<size;i++)
dest_one = source[ctr++];
for(i = ctr; i<size*2;i++)
dest_two = source[ctr++];

Can be done with one loop possibly. Or in a much different way. And we may need a +1 or -1 somewhere, I just quickly wrote it.

kanah

2011-11-02 17:56:18

This type of calculation is done all the time in audio signal manipulation due to multiple channels of information being compressed into a single byte stream. The optimal way to do this is in 2 loops as Sabzi has shown if you want it cut directly in half; you may also want to consider checking to verify that it is possible to split the array evenly.


Side note: I thought that arrays had to have a defined length during compile time in a C style language. In this code I expected to see a request for memory space to store the results (just incase the passed arguements aren't big enough). Second, I'm not sure if you have to specify that the arguements are passed by reference "&".

Brandon

2011-11-02 18:58:32

Well hey.. this isn't a very good example.. I know myself I can do a lot better but I just don't have time.. I gotta go build a website template for my team project for college but here try this and MODIFY IT of course..

I also put some debugging crap.. short n simple as it prints out both arrays in order of the numbers entered into them.. I did have to make another counter variable inorder to preserve ctr.. Ahh u can prob figure it out cuz u guys here at SRL are definitely smart.


#include <windows.h>
#include <iostream>

using namespace std;


void splitArray(int dest_one[], int dest_two[], int source[], int size)
{
int ctr = 0; // Counter that will go through the source[] array.
int i; // A counter
int x = 0; //Also a counter.

for(ctr; ctr<=(size/2); ctr++)
{
dest_one[ctr] = source[ctr];
}

for(i = ctr; i <= size; i++)
{
dest_two[x++] = source[i];
}

for(i = 0; i <= size/2; i++)
cout<<dest_one[i]<<endl;

for(i = 0; i <= ((size/2) - 1); i++)
cout<<dest_two[i]<<endl;
}

int main()
{
int source [] = {5, 11, 13, 6, 1, 3, 10};
int size = sizeof(source)/sizeof(int);
int dest_one[size/2];
int dest_two[size/2];

splitArray(dest_one, dest_two, source, size);

cin.get();
return 0;
}

Overtime

2011-11-03 10:27:08

I wish i was im just confused on array's atm :(


//
// Shifts the items in an array one place to the left
// Throws out the first element in the array and sets the last element
// in the array to zero.
//
// WRITE CODE TO COMPLETE THIS FUNCTION
//
void shift(int arr[], int size)
{
int i; // A counter


// shift the array



// Set the last element in the array to zero


}

Thats my second part to the first and I yet again got stuck :/

Nava2

2011-11-03 10:32:21

int newarr[ARR_LEN]
for (int j =1,i=0; j<ARR_LEN; j++,i++)
newarr=old[j];
newarr[ARRLEN-1]=0;

HyperSecret

2011-11-03 11:52:14

From: Overtime
I wish i was im just confused on array's atm :(


//
// Shifts the items in an array one place to the left
// Throws out the first element in the array and sets the last element
// in the array to zero.
//
// WRITE CODE TO COMPLETE THIS FUNCTION
//
void shift(int arr[], int size)
{
int i; // A counter


// shift the array



// Set the last element in the array to zero


}

Thats my second part to the first and I yet again got stuck :/


I don't see where you got stuck? Don't you usually have to attempt something before getting stuck? :D…

Try reading up on arrays, is there a certain something that you just don't understand about them or do you just not "get" arrays at all?