NO

Author Topic: function returning an array  (Read 2952 times)

Shiatzu

  • Guest
function returning an array
« on: October 23, 2008, 06:33:31 PM »
hi guys, i'm having this problem: i want to create a function that receives an array, then it generates a random and then returns the array with the first element of the array being the generated random and the previous values are appended to the end of the array.

i've searched a bit on the net and found this solution to return arrays:

create a typedef of the array an then return the typedef.

but i'm getting some errors.

this is what i have:

randomizer.h

Code: [Select]

#pragma once
#include <math.h>

typedef int array_mod[1023];

int rand(void);

array_mod randomizer(int array[]);

randomizer.c
Code: [Select]
#include "randomizer.h"


array_mod randomizer(int array[])
{
//the array size that is received as an argument is [1023]

int random = rand();
int array2[random];
int i=0;
int j=0;

//copy the first elements to a new array
for (i = 0; i != random; i++)
{

array2[i] = array[i];

}

//shifting the elements of the first array to the left
for (i = 0; i != 1023-random; i++)
{

array[i]=array[random];

}
//add the elements that were copied in the first for()

for(i=1023-random; i!=1023;i++){


array[i]=array2[j];
j++;
}

//copy the modified array into the typedef and return it
array_mod signal = array;

return signal;

}

the error i get is : Illegal return type 'array_mod'.

another question i have for you guys is how to do what i need, i.e. having a number ( for instance 5) and then append the first 5 elements of the array to the end, being that the array will then start with the index [5] element.
is my code ok for that or is there an easier\efficient way of doing it?

thanks in advance for any help.

Rui


« Last Edit: October 23, 2008, 06:53:26 PM by Shiatzu »

JohnF

  • Guest
Re: function returning an array
« Reply #1 on: October 24, 2008, 12:48:59 PM »
Well, I'm confused - and I'm sure you are too!

Let's start slowly.

Ist. Forget the .H file, it is not needed for what you are trying to do.

2nd. The typedef is not required.

3rd. There is no way to return an array in C, just use pointers.

4th. You need to limit the size of the array2 - see below for the expression using rand()

5th. You need to include stdlib.h


Code: [Select]
#include <stdio.h>
#include <stdlib.h> // for rand()

void randomizer(int array[])
{
//the array size that is received as an argument is [1023]
int random = rand() / (RAND_MAX / 1023 + 1);
int array2[random];
int i=0;
int j=0;

//copy the first elements to a new array
for (i = 0; i <= random; i++)
{
array2[i] = array[i];
}

// shifting the elements of the first array to the left
// do you mean to the right? This code does not achieve either.
for (i = 0; i <= 1023-random; i++)
{
array[i] = array[random];
}
}

int main(void)
{
int array_mod[1023];
randomizer(array_mod);
return 0;
}

Let's start with the above for now.

John

Shiatzu

  • Guest
Re: function returning an array
« Reply #2 on: October 26, 2008, 05:05:24 PM »
hey John

thank you for your reply.all sorted now:)