Inputting and Ordering Numbers in C

  • Thread starter Thread starter rambo3131
  • Start date Start date
Click For Summary

Discussion Overview

The discussion revolves around writing a C program that takes an uncertain number of numerical inputs and orders them from maximum to minimum. Participants explore various methods to handle input without relying on EOF, as the number of inputs can vary.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Homework-related

Main Points Raised

  • One participant expresses a need for a program that can handle an uncertain count of numbers without using EOF.
  • Another suggests using a terminating character or number in the input to signal the end of data instead of EOF.
  • A participant shares a code snippet that reads numbers until a non-number character is encountered, indicating a potential solution.
  • There is a request for clarification on whether the input will come from a file or the keyboard.
  • One participant mentions using a technique that does not rely on EOF and expresses interest in learning how to use EOF effectively.
  • Another participant provides an example of how to create a file and run the program with input redirection, explaining the use of Ctrl+D to signal EOF when reading from the keyboard.
  • A modified version of a previous code example is shared, which includes error checking for file opening and ensures robust reading of numbers.

Areas of Agreement / Disagreement

Participants have not reached a consensus on the best method to handle input without EOF, as multiple approaches are discussed, and some participants express uncertainty about their understanding of EOF.

Contextual Notes

Some participants are unclear about the specifics of EOF and how to implement it in their programs. There are also varying levels of familiarity with file handling in C, which may affect the discussion.

Who May Find This Useful

Individuals interested in programming in C, particularly those looking to handle dynamic input sizes and learn about file I/O operations.

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:
Technology 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?
 
rambo3131 said:
I want to write program that takes numbers as inputs

From a file or from the keyboard?
 
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?
 
  • #10
yes, Ctrl+D is so useful .Thank you so much.
 
  • #11
Besides, ı got your eof example sciurus.thanks a lot :)
 
  • #12
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;
}
 

Similar threads

  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 49 ·
2
Replies
49
Views
4K
Replies
18
Views
2K
Replies
3
Views
2K
  • · Replies 9 ·
Replies
9
Views
3K
Replies
14
Views
4K
  • · Replies 14 ·
Replies
14
Views
35K
Replies
20
Views
2K
  • · Replies 17 ·
Replies
17
Views
2K
  • · Replies 11 ·
Replies
11
Views
2K