Inputting and Ordering Numbers in C

  • Thread starter Thread starter rambo3131
  • Start date Start date
Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
11 replies · 3K views
rambo3131
Messages
18
Reaction score
0
Hi , I have a question about C. I want to write program that takes numbers as inputs and orders them from maximum to minimum. However, i ve a problem .Count of number is uncertain.I mean ,sometimes i will give the program 5 numbers ,sometimes 10 numbers or another.How can i do this without using eof since i don't know using eof.Please help me ...
 
Last edited:
Physics news on Phys.org


Sorry ,topic must be taking inputs in C .By the way I use linux.
 
You have to tell the program when to stop reading from the file. You can make the first entry in the file the number of numbers, use a terminating number or character in the file, or use EOF. If the file is supplied to you, you'll probably need to use EOF. Good luck, rambo3131.
 
rambo3131 said:
Hi , I have a question about C. I want to write program that takes numbers as inputs and orders them from maximum to minimum. However, i ve a problem .Count of number is uncertain.I mean ,sometimes i will give the program 5 numbers ,sometimes 10 numbers or another.How can i do this without using eof since i don't know using eof.Please help me ...
Can you give us the exact wording of the problem? It would help to know this information, which can help determine the direction to go.
 
rambo3131 said:
Hi , I have a question about C. I want to write program that takes numbers as inputs and orders them from maximum to minimum. However, i ve a problem .Count of number is uncertain.I mean ,sometimes i will give the program 5 numbers ,sometimes 10 numbers or another.How can i do this without using eof since i don't know using eof.Please help me ...

The following code will read numbers until unsuccesful.
That will be at EOF or when a non-number character is encountered.

Code:
#include <stdio.h>
void main(void)
{
    int number;
    while (1 == scanf("%d", &number))
    {
        printf(" %d", number);
    }
}

Is this what you want?
 
Thanks for all of you so much! I used " ı like serena" 's technique(without eof) and understood this. Moreover i want to learn about eof ( i think, this eof was important).How can i use eof?.Should i create file ? but i don't know this.Could you give an example code please?
 
Hi, rambo3131. I think this is correct. It's been awhile.

Code:
#include <stdio.h>
#include <io.h>
#include <stdlib.h>

int main(void)
{
  FILE *fp;
  int number; 

  fp=fopen("test", "r");
 
// A return value of EOF != 1, so this will terminate on EOF.

while (1 == fscanf(fp, "%d", &number)) 
  {
   printf("%d", number);
   }

  return 0;
}
 
rambo3131 said:
Thanks for all of you so much! I used " ı like serena" 's technique(without eof) and understood this. Moreover i want to learn about eof ( i think, this eof was important).How can i use eof?.Should i create file ? but i don't know this.Could you give an example code please?

I'm not sure what you do and do not know, so I'll just start somewhere.

EOF means "end-of-file" and if you try to read beyond the end of a file your reading function will act as if the special EOF-symbol has been read.

To create a file you can use e.g.:
$ echo "1 2 3 4" > test.txt

and you can run the program with
$ ./a.out < test.txt

Alternatively you can run your program without a file, effectively reading from the keyboard. You would do that like this:
$ ./a.out
1 2 3 4
Ctrl+D

The Ctrl+D here signals to Linux that you're at the end of your input, which is treated by your program as if the end-of-file is reached.

Is this the kind of stuff you want to know?
 
yes, Ctrl+D is so useful .Thank you so much.
 
Besides, ı got your eof example sciurus.thanks a lot :)
 
rambo3131 said:
Besides, ı got your eof example sciurus.thanks a lot :)

I have a slight modification of sciurus' code to make it more robust.

Code:
#include <stdio.h>

int main(void)
{
   FILE *fp;
   int number; 

   fp = fopen("test", "r");
   if (NULL != fp)
   {
      /* A return value of 1 means that 1 number has been 
       * succesfully parsed from the input
       */
      while (1 == fscanf(fp, "%d", &number)) 
      {
         printf("%d", number);
      }
      fclose(fp);
   }

   return 0;
}