6 Replies - 350 Views - Last Post: 19 April 2013 - 11:01 PM Rate Topic: -----

#1 TParker  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • 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  Icon User is offline

  • Engineer
  • member icon

Reputation: 1091
  • View blog
  • Posts: 4,572
  • 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

Was This Post Helpful? 0
  • +
  • -

#3 TParker  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • 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?
Was This Post Helpful? 0
  • +
  • -

#4 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1091
  • View blog
  • Posts: 4,572
  • 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.
Was This Post Helpful? 0
  • +
  • -

#5 TParker  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • 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

Was This Post Helpful? 0
  • +
  • -

#6 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1091
  • View blog
  • Posts: 4,572
  • 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

Was This Post Helpful? 0
  • +
  • -

#7 TParker  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • 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
Was This Post Helpful? -1
  • +
  • -

Page 1 of 1