Eskipo
Eskipo

Reputation: 51

Printing an array from another function in C

I have some problems understanding how to do this kind of stuffs. I have already made a function that does the logical part like declaring an Integer that get the value of the array[i] and this number is random picked from the array(i think). Anyway here is the code that I have done:

void array_test( int arr[]  , int size)
{
    int i;
    int random_number;

    for(i = 0; i<size; i++){
         random_number = (rand() %15 + 1);
         arr[i] = random_number;
    }

}

Now my question is how do I print the result(the code works and it does print out 15 different numbers between 1 and 15. I have defined the size to 15 and it is on the function prototype parameter) using another function? The function takes a char output[] as one parameter, an array of type int, and a size of type int. Looks like this output_result(char output[], int arr[], int size).

The thing is that I don't understand how I'm supposed to call the stuff from the previous function to this and then show it to the user. I admit that this is an assignment but I have tried many things and nothing works. The thing is that I have to printf all the arrays(?) value by using a for-loop. It's here it just get messy. I've tried to create the wheel again ,so to say, by copy pasting the code from the function array_test but then the function array_test would be unnecessary to implement. No pointers shall be used until next assignment.

Here is the main function:

int main() {
  int arr[SIZE]; 

  srand(time(0)); 
  array_test(arr, SIZE);
  print_array("This is an array\0", arr, SIZE);

  return 0; 
}

Upvotes: 0

Views: 3016

Answers (2)

Chris Cirefice
Chris Cirefice

Reputation: 5795

If you want to print the entire array, you can do the following:

EDIT: Based on your main function that you gave us, your print_values_ver1 function could look something like this:

In your main function, to find array size you would use this:

int size = sizeof(array) / sizeof(array[0]);

Then pass that you your print function like:

print_values_ver1("Some text\0", arr, size);

and your function definition would look something like:

print_values_ver1(char[] output, int arr[], int SIZE) {

    for (int z = 0; z < SIZE; z ++) {
        printf("%d\n", arr[i]);
    }
}

Note that %d is a format specifier of the printf function (see documentation). And \n is a newline character.

This code iterates through each element of the array and prints the value (one value per line).

Upvotes: 4

ryyker
ryyker

Reputation: 23208

Like this: (comments in-line explain whys-)

[EDIT] adds output_results()

#include <ansi_c.h>

void array_test( int arr[]  , int size);
void output_result(int a[], int size);

int main(void)
{
    int arr[15]; 
    int size = sizeof(arr)/sizeof(arr[0]);
    array_test(arr, size);
    output_result(arr, size); 
    getchar();
    return 0;   
}

void output_result(int a[], int size)
{
    int i=0;
    for(i=0;i<size;i++) printf("a[%d] is:%d\n", i, a[i]);
}

void array_test( int arr[]  , int size)
{
    int i;
    int randomly_picked_number;


    for(i = 0; i<size; i++){
         randomly_picked_number = (rand() %15 + 1);
             arr[i] = randomly_picked_number;



    }

}

Upvotes: 1

Related Questions