# Returning a New Array from Void Function

Page 1 of 1

## 6 Replies - 544 Views - Last Post: 19 April 2013 - 11:01 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=319036&amp;s=ca9199bb4b5f5660f4879c7d9c3f68ff&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 TParker

Reputation: -1
• Posts: 30
• Joined: 21-March 13

# Returning a New Array from Void Function

Posted 19 April 2013 - 10:07 PM

Hello!

I am trying to normalize an array, but I don't know how to return the normalized array. Here is my code:

```#include <stdio.h>
#include <math.h>
#include <stdlib.h>
void aprint( int a[], int n)
{
int i;
for( i = 0; i < n; ++i)
printf( " %i", a[i]);
putchar('\n');
}
void normalize( double x[], int num_pts)
{
int k;
for( k=1; k<num_pts; k++)
x[k] = (x[k] + 2.0)/(4.0);
return ???;
}
int main(void)
{
int i, x[4]={-2,-1,2,0};
int max, min;
aprint( x, 4);
max=0;
min=x[0];
for(i=0;i<4;i++)
{
if(x[i]>max)
{
max=x[i];
}
if(x[i]<min)
{
min=x[i];
}
}
printf("\nMax: %d",max);
printf("\nMin: %d",min);

return 0;
}
```

As you can see, the array starts with the numbers -2,-1,2,0. After normalization, I am trying to get a new array with the numbers 0,0.25,1,0.5. What do I put in the ??? area (in the function normalize) to get back a new array?

This post has been edited by TParker: 19 April 2013 - 10:09 PM

Is This A Good Question/Topic? 0

## Replies To: Returning a New Array from Void Function

### #2 jjl

• Engineer

Reputation: 1169
• Posts: 4,785
• Joined: 09-June 09

## Re: Returning a New Array from Void Function

Posted 19 April 2013 - 10:11 PM

No need to return the array, arrays are naturally passed via pointer (reference). Any changes made to that array in the normalize function will reflect in the caller function.

This post has been edited by jjl: 19 April 2013 - 10:11 PM

### #3 TParker

Reputation: -1
• Posts: 30
• Joined: 21-March 13

## Re: Returning a New Array from Void Function

Posted 19 April 2013 - 10:14 PM

Wait so I can just take out return?

What statement would I need in main to get the new numbers?

### #4 jjl

• Engineer

Reputation: 1169
• Posts: 4,785
• Joined: 09-June 09

## Re: Returning a New Array from Void Function

Posted 19 April 2013 - 10:16 PM

Arrays are not copied over when you pass them to a function. Rather, a pointer to the start of the array is passed. The memory pointed to by the pointer is the same memory of the array declared in main.

You don't need a return statement. Try printing the values after calling normalize, you will see.

### #5 TParker

Reputation: -1
• Posts: 30
• Joined: 21-March 13

## Re: Returning a New Array from Void Function

Posted 19 April 2013 - 10:24 PM

```#include <stdio.h>
#include <math.h>
#include <stdlib.h>
void aprint( int a[], int n)
{
int i;
for( i = 0; i < n; ++i)
printf( " %i", a[i]);
putchar('\n');
}
void normalize( double x[], int num_pts)
{
int k;
for( k=1; k<num_pts; k++)
x[k] = (x[k] + 2.0)/(4.0);
}
int main(void)
{
int i, x[4]={-2,-1,2,0};
int max, min;
aprint( x, 4);
max=0;
min=x[0];
for(i=0;i<4;i++)
{
if(x[i]>max)
{
max=x[i];
}
if(x[i]<min)
{
min=x[i];
}
}
printf("\nMax: %d",max);
printf("\nMin: %d",min);

int normalized = normalize(x,4);
printf("%d \n", normalized);
return 0;
}
```

Alright, this is what I have, but I am getting errors...

Have any idea whats wrong?

This post has been edited by TParker: 19 April 2013 - 10:21 PM

### #6 jjl

• Engineer

Reputation: 1169
• Posts: 4,785
• Joined: 09-June 09

## Re: Returning a New Array from Void Function

Posted 19 April 2013 - 10:32 PM

Kinda hard to tell without you posting the error messages?

If you are conforming to the ANSI standard, all variables have to be declared at the top of the corresponding function, however you declare normalized at the bottom of main.

Also, the normalized function doesn't return anything, it just modifies the input array. Therefore, this statement is illegal

```int normalized = normalize(x,4);

```

If you want to print the normalized values, loop over the values of the x array and print them.

This post has been edited by jjl: 19 April 2013 - 10:33 PM

### #7 TParker

Reputation: -1
• Posts: 30
• Joined: 21-March 13

## Re: Returning a New Array from Void Function

Posted 19 April 2013 - 11:01 PM

Sent you a PM