Inputting and Ordering Numbers in C

  • Thread starter rambo3131
  • Start date
In summary: 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
  • #1
rambo3131
18
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
  • #2


Sorry ,topic must be taking inputs in C .By the way I use linux.
 
  • #3
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.
 
  • #4
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.
 
  • #5
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?
 
  • #6
rambo3131 said:
I want to write program that takes numbers as inputs

From a file or from the keyboard?
 
  • #7
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?
 
  • #8
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;
}
 
  • #9
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;
}
 

FAQ: Inputting and Ordering Numbers in C

1. How do I input numbers in C?

To input numbers in C, you can use the scanf() function. This function takes in user input and stores it in a variable. For example, if you want to input an integer, you can use the format specifier "%d" with the scanf() function.

2. How do I order numbers in C?

To order numbers in C, you can use the built-in function qsort(). This function takes in an array of numbers and sorts them in ascending order. It requires a comparison function to be passed in as a parameter, which determines the sorting order.

3. Can I input and order numbers at the same time in C?

Yes, you can input and order numbers at the same time in C. You can first input the numbers using the scanf() function and store them in an array. Then, you can use the qsort() function to order the numbers in the array.

4. How do I handle input errors in C?

You can handle input errors in C by checking the return value of the scanf() function. It returns the number of successfully read items, so if it returns 0, it means there was an error in the input. You can also use the fflush() function to clear the input buffer in case of errors.

5. Can I input and order decimal numbers in C?

Yes, you can input and order decimal numbers in C. You can use the format specifier "%f" with the scanf() function to input a float or double value. For ordering decimal numbers, you can use the qsort() function with a comparison function that takes in float or double values.

Similar threads

Back
Top