-6

I want to get a variable from user and set it for my array size. But in C I cannot use variable for array size. Also I cannot use pointers and * signs for my project because i'm learning C and my teacher said it's forbidden. Can someone tell me a way to take array size from user?

At last, I want to do this two projects: 1- Take n from user and get int numbers from user then reverse print entries. 2- Take n from user and get float numbers from user and calculate average. The lone way is using array with variable size.

<3

EDIT (ANSWER THIS):

Let me tell full of story.

First Question of my teacher is: Get entries (int) from user until user entered '-1', then type entry numbers from last to first. ( Teacher asked me to solve this project with recursive functions and with NO any array ) Second Question is: Get n entries (float) from user and calculate their average. ( For this I must use arrays and functions or simple codes with NO any pointer )

M e T
  • 1
  • 2
  • if I remember correctly, variable sized arrays are allowed when compiling with -std=c99, but normally you would want to use malloc(n * sizeof(datatype)) when making arrays. The problem is that the second method requires use of pointers – Patrick Mar 15 '19 at 14:11
  • If VLA is not allowed, the next option on the list is dynamic memory allocation using malloc() family. But that involves pointers. I dont know of other ways of allocating memory based on a variable. – machine_1 Mar 15 '19 at 14:13
  • Finally, you can declare a large, global array but only use a part of it. – Paul Ogilvie Mar 15 '19 at 14:14
  • 1
    @paul it doesn't have to be global. You can make it static & local. – machine_1 Mar 15 '19 at 14:16
  • 3
    Get a better teacher - pointers and pointer operations are *fundamental* to C programming and should be learned early on. As for arrays with user-defined size, if you're using a compiler that supports C99 or later (gcc, clang) you can use a runtime variable to specify the array size. If you're using MSVC or Turbo C which only support C89, then you're out of luck - the only option is to use dynamic memory allocation. – John Bode Mar 15 '19 at 14:27
  • 1
    What does this mean: "But in C I cannot use variable for array"? Did the teacher tell you not to use it or do you think, it is not possible? – Gerhardh Mar 15 '19 at 14:40
  • Read more about [C dynamic memory allocation](https://en.wikipedia.org/wiki/C_dynamic_memory_allocation) – Basile Starynkevitch Mar 15 '19 at 14:41
  • "*Let me tell full of story.*" so your question in fact is two (implementationwise) unrelated questions? – alk Mar 15 '19 at 14:49
  • "*Get n entries ...*" `n` has to be read from the user as well, or is it a constant? – alk Mar 15 '19 at 14:51
  • Guys I know pointers and malloc and .. but for this project I have no permission to use pointers and malloc. for first task I must not use arrays and for second task I must use arrays with no pointer – M e T Mar 15 '19 at 15:03
  • VLAs are optional in recent versions of C (2011 and later), and were not part of the 1989 C standard at all. There are real-world implementations that do not support VLAs. If using a C compiler that does not support VLAs, and if also constrained not to use pointers (which is rather arbitrary), there is no way to have an array with size determined at run time. Either way, there is no such thing as an infinite array index in C. – Peter Mar 15 '19 at 15:07
  • I'm using CodeBlocks last version and I don't anything about IDE. – M e T Mar 15 '19 at 15:11
  • I understand average now. But reverse project not. – M e T Mar 15 '19 at 15:12
  • Guys, I Think we don't need infinite array index. My program must get entries until entry was '-1' then type all entries from last to first. It just need recursive function. Just tell me how to write it. what is it. English typing killed me. – M e T Mar 15 '19 at 15:16

3 Answers3

4

Modern C has variable size arrays, as follows:

void example(int size)
{
    int myArray[size];
    //...
}

The size shouldn't be too large because the aray is allocated on the stack. If it is too large, the stack will overflow. Also, this aray only exists in the function (here: funtion example) and you cannot return it to a caller.

Paul Ogilvie
  • 25,048
  • 4
  • 23
  • 41
1

I think your task is to come up with a solution that does not use arrays.

For task 2 that is pretty simple. Just accumulate the input and divide by the number of inputs before printing. Like:

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

int main()
{
    float result = 0;
    float f;
    int n = 0;
    printf("How many numbers?\n");
    if (scanf("%d", &n) != 1) exit(1);
    for (int i=0; i < n; ++i)
    {
        if (scanf("%f", &f) != 1) exit(1);
        result += f;
    }
    result /= n;
    printf("average is %f\n", result);
    return 0;
}

The first task is a bit more complicated but can be solved using recursion. Here is an algorithm in pseudo code.

void foo(int n)  // where n is the number of inputs remaining
{
    if (n == 0) return;     // no input remaining so just return
    int input = getInput;   // get next user input
    foo(n - 1);             // call recursive
    print input;            // print the input received above
}

and call it like

foo(5);  // To get 5 inputs and print them in reverse order

I'll leave for OP to turn this pseudo code into real code.

Support Ukraine
  • 42,271
  • 4
  • 38
  • 63
  • "*`print input`*"? – alk Mar 15 '19 at 14:48
  • I cannot understand your codes. for first answer, I've entered 0 and 20 and program returns 0 for me as average. – M e T Mar 15 '19 at 14:53
  • for second answer I don't know what did you do. be advise, comment your codes i am a newbie C developer. – M e T Mar 15 '19 at 14:54
  • Is it possible to write project and comment it please? I'm a newbie and I cannot write programs at best I can just read and understand and copy solutions. – M e T Mar 15 '19 at 14:57
  • Sorry my bad English. – M e T Mar 15 '19 at 14:58
  • Thanks about Average. now first task is remaining. – M e T Mar 15 '19 at 15:12
  • @MeT You write: " I've entered 0 and 20 and program returns 0" If you start by entering 0 it means that the user wants to input zero floats! What is the average of zero floats? The idea is that the user enters something like "3 2.0 3.0. 4.0" and the program prints the average of the three floats – Support Ukraine Mar 15 '19 at 16:04
  • @MeT I added some comment. But notice that you have to work with this to learn. You can't just say "I cannot write programs". You want to learn to write programs so start practicing :-) – Support Ukraine Mar 15 '19 at 16:08
  • Ok I know. Now I must write a program with recursive function to get numbers and write them from last to first when user typed -1. what can I do ??? – M e T Mar 15 '19 at 18:50
0

You can actually use variable sized arrays. They are allowed when compiling with -std=c99

Otherwise, you can over-allocate the array with an arbitrary size (like an upper bound of your actual size) then use it the actual n provided by the user.

I don't know if this helps you, if not please provide more info and possibly what you have already achieved.

sebastian
  • 412
  • 1
  • 4
  • 14