-1

I need help with this problem that I was given at my school. The problem is writen in Serbian, I'll try my best to translate it.

Write a program that enters n elements of a one-dimensional array and then displays their ordinal number, index and value (see test example)

screenshot

The test example is in Serbian as well, but I think you can guess what do you need to do. Here is what I tried to do:

#include <stdio.h>
#include <stdlib.h>
int main()
{
    int n,i,clan,broj=0,b,a;
    a=1;
    printf("Unesi broj elementa niza:\n");
    scanf("%d", &n);
    for (i=1;i<=n;i++)
    {
        printf("Unesi %d clan niza:\n", i);
        printf("a[%d]=", broj);
        broj++;
        scanf("%d", &clan);
    }
    for (b=1;b<=n;b++)
    {
        printf("%d. clan niza je a[%d]=%d\n", a, b, clan);
        a++;
    }
    return 0;
}

Everything works fine except that "clan" will only show as the latest entered number.

lxop
  • 7,596
  • 3
  • 27
  • 42
UrosHD
  • 13
  • 1
  • 3
    You will never get an answer with that question. "Make me a program for this problem please" Please make an attempt at describing your question/problem. – Joshua Schlichting Jun 04 '20 at 23:01
  • 1
    I am trying to compile a program for the task above, but I am running into a problem, you see, in the given example, the person must enter a series of numbers until it is equal to "n" and then those numbers must be printed back at them in ordinal order, as in the image above, but the problem is, only the last entered number will be printed back at me, and I can't find a solution how to fix it, I hope you now understand what is going on – UrosHD Jun 04 '20 at 23:13
  • @JoshuaSchlichting - OP has been edited twice since your comment. Its certainly up to you, but if the conditions of the original post are improved in your opinion, would you consider removing your comment? – ryyker Jun 05 '20 at 16:54

2 Answers2

2

"...only the last entered number will be printed back at me, and I can't find a solution..."

Use an array.

The variable clan is not able to contain more than one int value at a time, however, the array variable int clan[n]; can hold up to n int values. Below is your code modified to accommodate n elements of a clan array using a VLA:

int main(void)
{
    int n,i,broj=0,b,a;//remove clan
    a=1;
    printf("Unesi broj elementa niza:\n");
    if(scanf("%d", &n) == 1)//test for success here, exit if fail
    {
        int clan[n];//use user input value to help create array of clan
        for (i=0;i<n;i++)//array index is from 0 to n-1
        {
            printf("Unesi %d clan niza:\n", i);
            printf("a[%d]=", broj);
            broj++;
            scanf("%d", &clan[i]);//modify to populate 1 element of clan array
        }
        for (b=0;b<n;b++)
        {
            printf("%d. clan niza je a[%d]=%d\n", a, b, clan[i]);//modify to oupput 1 element of clan array
            a++;
        }
    }
    else
    {
        printf("scanf() call failed.  Exiting.");
    }
    return 0;
}
ryyker
  • 22,849
  • 3
  • 43
  • 87
1

You are storing all your values into the same variable, clan. Of course, each assignment overwrites the previous value - it is one variable, not a stack.
Reading it five times in a loop gives each time that last value.

Aganju
  • 6,295
  • 1
  • 12
  • 23