Sentinel value how to correct my code to make it more efficient

In summary, The conversation discusses a task in "C" environment involving reading a text file with a sequence of positive integers, terminated by a sentinel value of -1. The code provided has bugs and the speaker asks for help in improving it. The bugs include not initializing the variable numbers and not checking the return value of fscanf. The speaker also mentions that the global variables num and total are automatically initialized to 0 but it would be better to explicitly initialize them for clarity.
  • #1
sh4rif
4
0
Hello to all

I have been given a task in "C" environment ... to read a text file with a sequence of positive integers the sequence should be terminated with a sentinel value of -1

Values in text file is : 6 9 17 4 12 8 7 -1

We have to read the integers from text file, display the integers, total, average. the value -1is a sentinel value and is not part of the sequence.

I’ve created a code this works fine but I want a better code then this I hard quoted -1 could anyone please help me to improve my code please.

thanks once again
Usman
below is my code

Code:
#include <stdio.h>

char filename[] = "C:\\numbers.txt";

int numbers, total, num;
float average;

FILE *fp;

main()
{
      if((fp = fopen(filename, "r")) == NULL)
           printf("\n\n\t\t\tError opening file.");
      else
      {
           printf("\n\n\t\t");
           
           while(numbers>=0){
                fscanf(fp, "%d", &numbers);
                
                if(numbers != -1)
                {
                     printf("%4d", numbers);
                     total = total + numbers;
                     num++;
                }
       
           }
           average = (float) total/num;
           printf("\n\n\t\tTotal is %6d", total);
           printf("\n\n\t\tAverage is %7.2f", average);
      }
      getchar();
}
 
Technology news on Phys.org
  • #2
Your program has a bug. Your while loop checks to see if numbers >= 0, but the first time through the loop, numbers is undefined. Better practice would be to initialize it to a known value such as 1.

I'll take a closer look at your code later.
 
  • #3
It has another bug, not as critical as the one stated above, but still a very bad practice:

you're not checking the return value of fscanf. Don't assume that things went ok. What if the end-of-file is reached? What if there is a file error? What if one of the numbers is not actually a number?
 
  • #4
thanks guys i know i have a bug in my code i can't think of anything how to check do you have any tips to sort this out?... I would really apperciate any help... thanks one more time

take care bye
 
  • #5
See posts #2 and #3.

In addition to what was already mentioned, you should explicitly initialize num and total to 0. You might not realize it, but those variables are automatically initialized to 0 by virtue of their being global variables. If you had declared those variables inside your main function, you would be getting garbage results.
 

1. What is a sentinel value?

A sentinel value is a specific value that is used to signal the end of a sequence or to indicate that a certain condition has been met. In programming, sentinel values can be used to control loops or to stop the execution of a program.

2. Why is it important to use sentinel values?

Using sentinel values can help make code more efficient by reducing the number of comparisons or conditions that need to be checked. They can also help prevent unexpected errors or infinite loops.

3. How do I choose a sentinel value?

The chosen sentinel value should be unique and should not be a valid input for the program. It should also be easily distinguishable from other values in the sequence.

4. Can a sentinel value be changed during the execution of a program?

Yes, it is possible to change the sentinel value during the execution of a program if necessary. However, it is important to ensure that the new value does not conflict with any other values in the sequence.

5. Are there any alternatives to using sentinel values?

Yes, there are other methods that can be used to control loops or indicate the end of a sequence, such as using a counter or a boolean variable. However, sentinel values can often be a simpler and more efficient solution.

Similar threads

  • Programming and Computer Science
Replies
1
Views
936
  • Programming and Computer Science
Replies
5
Views
1K
  • Programming and Computer Science
Replies
4
Views
1K
  • Programming and Computer Science
Replies
11
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
0
Views
2K
  • Programming and Computer Science
Replies
6
Views
3K
  • Programming and Computer Science
Replies
16
Views
8K
  • Programming and Computer Science
2
Replies
54
Views
4K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
2K
Back
Top