-1

Currently, I am asking user to specify number of input values being specified. This the code for it:

#include<stdio.h>
#include<math.h>
#include<string.h>
void main()
{
    int i,n;
    printf("\nHow many record you will enter: ");
    scanf("%d",&n);
    float x[n];
    printf("\n\nEnter the values of velocity (m/s):");
    for(i=0; i<n; i++)
    {
        scanf("%f",&x[i]);
        printf("\n%f",x[i]);
    }   
}

The code runs fine. But, I want to write code in such a way that it will calculate 'n' by scanning the input (numbers separated by space, not necessary one space between each number) without asking the user. Can you suggest me a way for it.

PS: I am new to coding

Thanks in advance

  • Suggestion: prefer `int main(void)` over `void main()`. – Sourav Ghosh Apr 03 '15 at 11:54
  • Why are you printing a newline *before* the text you want to print? I've seen this more and more among beginners, and it doesn't really make any sense to me. Mostly because output to `stdout` (which is what `printf` writes to) is by default *line buffered*, which means the output is flushed (i.e. actually printed) on newline. So if you print two lines with newline at the beginning then only the first line would be displayed initially. – Some programmer dude Apr 03 '15 at 11:55
  • I can do what you wanted using string, but taking input as int? Is it possible? i think, not... :( – Mukit09 Apr 03 '15 at 11:57
  • As for your problem, if you don't care about *storing* the values then just read in an infinite loop until `scanf` returns `EOF` or `0`. Otherwise you might want to find a tutorial on *pointers* and the [`malloc`](http://en.cppreference.com/w/c/memory/malloc) and [`realloc`](http://en.cppreference.com/w/c/memory/realloc) functions. – Some programmer dude Apr 03 '15 at 11:58
  • Or you simply look up all the other "related" questions. This question was asked many times before. – BitTickler Apr 03 '15 at 11:59
  • Dynamic memory allocation using `malloc`/`calloc`/`realloc` is what you want. – Spikatrix Apr 03 '15 at 11:59
  • http://stackoverflow.com/questions/28233450/in-c-parsing-a-string-of-multiple-whitespace-separated-integers/28233856#28233856 – BitTickler Apr 03 '15 at 12:04
  • Can somebody explain it with example. I am struggling to put EOF as condition to while loop – Atish Dixit Apr 03 '15 at 13:56

1 Answers1

0

You can have a look at fgets() and strtok(). Combining both of them , you can design as per your target. Also, you may need to know and use malloc() and free() to make use of dynamic memory allocation.

Maybe this answer can help you.

Community
  • 1
  • 1
Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
  • I am trying to use scanf function to find the end of the input as below: #include #include #include void main() { int i,n; float x[10]; printf("\n\nEnter the values of velocity (m/s):"); while(scanf("%f",&x[i])==1) { scanf("%f",&x[i]); i++; n++; } for (i=0; i>n; i++) { printf("\n%f",x[i]); } } The while loop is not ending! – Atish Dixit Apr 03 '15 at 14:13
  • I saw that already and didn't get it. I understood all the steps explained and studied all the functions mentioned there. But I am not sure how to use it for my problem. – Atish Dixit Apr 03 '15 at 14:25