0

I am trying to make two separate functions, one for a reading of an array, and another one for prining it. My code looks something like this:

#include <stdio.h>
#include <stdlib.h>

void read(int n, int v[100])
{
    scanf("%d",&n);
    for(int i=0;i<n;i++)
    {
        scanf("%d",&v[i]);
    }
}

void print(int n, int v[100])
{
    for(int i=0;i<n;i++)
    {
        printf("%d",v[i]);
    }
}

int main()
{
    int v[100];
    int n;
    read(n,v);
    print(n,v);
}

But if i read something like this

5
1
2
3
4
5

it prints this:

12345167773430408951321408978481140419686004089785612740906704021677734340894

and other numbers. Any suggestions?

5
  • 2
    read() doesn't set the value of n in main--it sets a local copy. You need to make read take a pointer and pass in &n. Commented Jan 24, 2018 at 22:12
  • 4
    read(n,v); and print(n,v); are using the uninitiaised variable n. Commented Jan 24, 2018 at 22:12
  • You may want to print some whitespace between your numbers. %d doesn't print any. Commented Jan 24, 2018 at 22:13
  • What @LeeDanielCrocker said. Or just have read() return the count instead of using a pointer parameter. Commented Jan 24, 2018 at 22:13
  • The array int v[100]; printed in void print is also unintialised. Please understand that local variables defined in a function have indeteminate value. Commented Jan 24, 2018 at 22:18

4 Answers 4

3

Here is a program that does what you want it do to.

#include <stdio.h>

// Function Prototypes Start
void array_reader(int array_length, int array[]);
void array_printer(int array_length, int array[]);
// Function Prototypes End


int main(void) {

    int max_amt_to_scan;
    printf("How many numbers would you like to scan in?\n");
    scanf("%d", &max_amt_to_scan);

    int num_array[max_amt_to_scan];

    printf("Please enter %d numbers!\n", max_amt_to_scan);
    array_reader(max_amt_to_scan, num_array);

    printf("The numbers you entered were:\n");
    array_printer(max_amt_to_scan, num_array);

    return 0;
}


// Scans or reads in numbers, into an array
void array_reader(int array_length, int array[]) {

    int i = 0;
    while (i < array_length) {

        scanf("%d", &array[i]);
        i++;
    }
}


// Prints out an array of numbers
void array_printer(int array_length, int array[]) {

    int i = 0;
    while (i < array_length) {

        if (i == (array_length - 1)) {
            printf("%d\n", array[i]);
        } else {
            printf("%d, ", array[i]);
        }
            i++;
    }
}
Sign up to request clarification or add additional context in comments.

Comments

1

You don't initialize n in main. The n in main and the n in read are different variables, so changing n in read doesn't affect the n in main, which remains uninitialized and hence you have an undefined behaviour in print.

You have to either pass n as a pointer or read must return n:

void read(int &n, int v[100])
{
    scanf("%d",n);
    for(int i=0;i<*n;i++)
    {
        scanf("%d",&v[i]);
    }
}

int main()
{
    int v[100];
    int n;
    read(&n,v);
    print(n,v);
}

or

int read(int v[100])
{
    int n;
    scanf("%d",&n);
    for(int i=0;i<n;i++)
    {
        scanf("%d",&v[i]);
    }
    return n;
}

int main()
{
    int v[100];
    int n = read(v);
    print(n,v);
}

Comments

0

This is what it should look like:

void read(int *array, int size)
{
    printf("Now enter %d values:\n", size);
    for(int i = 0; i < size; i++)
    {
        scanf("%d", array + i);
        /* array + i = &(array[i]) */
    }
}

void print(int *array, int size)
{
    printf("Here is your array:\n");
    for(int i = 0; i < size; i++)
    {
        printf("%d%c", array[i], i == size - 1 ? '\n' : ' ');
        /* ternary puts spaces and \n at the end */
    }
}

int main()
{
    int size;
    printf("Enter array size :\n");
    scanf("%d", &size);
    int array[size];  /* Keep one array */
    read(array, size);
    print(array, size);
}

7 Comments

@FredLarson Wow thanks, i never realized that i was wrong. I am sure that i have it seen it written like that somewhere, too many wrong info on internet nowadays...
Maybe you were using the same lousy book quoted in the question.
@FredLarson No, i actually never opened a "book", only online resources. I suspect wrong info just survives for a long time.
Learning C from online resources is probably not a good idea. Note that C functions are call by value, so I suspect that a function prototype like void read(int n, int v[100]); confuses many into thinking that the value of the array is passed in the function call. Yet: 1) array types in function prototypes are adjusted to appropriate pointers, making the actual prototype void read(int n, int *v);, and 2) arrays decay to pointers to their first elements in most expressions, so read(n, v); passes a pointer to the first element of v by value (a copy) to read().
In all expressions except sizeof, _Alignof and when used with the unary & operator. 6.3.2.1 Lvalues, arrays, and function designators (p3)
|
-3

Ok, i solved it by declaring the variable n and v[100] globally. Thanks.

EDIT

I also tried to use the pointer and it looks something like this:

#include<stdio.h>
void read(int a[], int* n)
{
    scanf("%d",n);
    for(int i=0;i<*n;i++)
    {
        scanf("%d",&a[i]);
    }
}
void print(int a[], int n)
{
    for(int i=0;i<n;i++)
        printf("%d ",a[i]);

}
void main()
{
    int a[20],n;
    read(a,&n);
    print(a,n);
}

And it works.

2 Comments

Global variable are usually not the answer. Here, they are not the answer.
Very bad solution, a cop-out. Please learn how to handle local variables.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.