-6

So I was pondering about this problem in C using only stdio.h -

I have to write a program that reads an integer p and then reads p integers. Now I have to use these p integers and perform another operation on them to get a final answer, which my main program will return.

The problem is I don't know how to perform an operation on a variable number of integers. I tried using pointers but they don't seem to work.

Can anyone help please?

#include <stdio.h>

int main(){
    int i, p, n;
        scanf ("%d", &p);
        for (n=0; n<p; n++){
            int a;
            scanf ("%d", &a);
            int *k=&a;
        }
        for (n=0; n<p; n++){
            int a=*k;
            if (a==0)
                i=0;
            else i=1;
        }
    return i;
}

What I want to do here is to read a certain integer p, then read p integers a, and if at least one of these as is 0, my answer is 0. Otherwise it's 1.

But the compiler says *k is not defined for the second for loop. What should I do?

J...S
  • 5,079
  • 1
  • 20
  • 35
Gravemind
  • 15
  • 3

2 Answers2

0

I think you can do this with a fixed number of variables. Like

int p, a, ans=1;
scanf("%d", &p); //read number

for(int r=0; r<p; ++r)
{
    scanf("%d", &a);
    if(a==0)
    {
        ans=0;
        break;
    }
}   
printf("\nAnswer is: %d", ans);

ans variable holds the answer. First you read the number of ints into p and then read p number of ints using a loop.

During each iteration of the loop, you read into the same variable a. You just need to know if at least a single input is zero.

ans is initially set to 1 and is changed to 0 only if a zero is input. If zero is input, the control exits the loop immediately because of the break statement.

You may want to check the return value of scanf() and other error checking.

Various ways to use an unknown number of variables is mentioned in the comments.

If you can use C99, variable length array are allowed. Like

int p;
scanf("%d", &p);
int arr[p];

If dynamic memory allocation is used, you could use malloc() or calloc() which are in stdlib.h.

See this.


And the pointer k in

    for (n=0; n<p; n++){
        int a;
        scanf ("%d", &a);
        int *k=&a;
    }
    for (n=0; n<p; n++){
        int a=*k;
        if (a==0)
            i=0;
        else i=1;
    }

is a local variable of the first loop and is hence visible only to that loop and is invisible to the second loop. k's scope is limited to the first loop. See this.

If you want a variable to be visible in both the loops, declare it outside both.

J...S
  • 5,079
  • 1
  • 20
  • 35
0

First things first, I'll help you understand the compiler error you talked about:

k is not defined for the second for loop

// some code

for (n=0; n<p; n++){
    int a;
    scanf ("%d", &a);
    int *k=&a;   // Definition of k is here, and is restricted to this block, 
}
for (n=0; n<p; n++){
    int a=*k;   // Its undefined here
    if (a==0)
       i=0;
    else i=1;
}
// some code

Explanation: Read the comment in the code. What I meant by block is the code between the upper '{' and lower '}'. Beyond these, anywhere in code, k is undefined. Hence in second loop, the k is undefined. You should change your main() function to:

int main() 
{
    int i, p, n, k = 0;
    scanf ("%d", &p);
    for (n=0; n<p; n++) {
        int a;
        scanf ("%d", &a);
        int *k=&a;
    }
    for (n=0; n<p; n++) {
        int a=*k;
        if (a==0)
            i=0;
        else 
            i=1;
    }
    return i;
}

Now to address your next issue, I guess you might want to study variadic functions in C. I may be wrong in understanding your problem statement, but my take on it is that you are willing to pass multiple arguments to a function. For example,

At some time, you want to call function as:

function(Count_args, 1, 2, 3, 4, 5);

and sometimes as:

function(Count_args, 10);

If I am right, you might want to take a look at this example on the same reference I cited above. For making your life easier, I am adding the same code here:

#include <stdarg.h>
#include <stdio.h>

int
add_em_up (int count,...)
{
    va_list ap;
    int i, sum;

    va_start (ap, count);         /* Initialize the argument list. */

    sum = 0;
    for (i = 0; i < count; i++)
    sum += va_arg (ap, int);    /* Get the next argument value. */

    va_end (ap);                  /* Clean up. */
    return sum;
}

int
main (void)
{
    /* This call prints 16. */
    printf ("%d\n", add_em_up (3, 5, 5, 6));

    /* This call prints 55. */
    printf ("%d\n", add_em_up (10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10));

    return 0;
}

If you are willing to know that latest standard says about having variable arguments to a function, refer C11, section 7.16 Variable arguments , it does help!

WedaPashi
  • 3,561
  • 26
  • 42