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

  • Thread starter Thread starter sh4rif
  • Start date Start date
  • Tags Tags
    Code Value
Click For Summary

Discussion Overview

The discussion revolves around improving a C program designed to read a sequence of positive integers from a text file, terminating with a sentinel value of -1. Participants are focused on identifying bugs and enhancing the efficiency and reliability of the code.

Discussion Character

  • Technical explanation
  • Debate/contested

Main Points Raised

  • Usman presents a code snippet that reads integers from a file and calculates their total and average, but seeks improvements.
  • One participant points out a bug related to the initialization of the variable 'numbers', suggesting it should be initialized to a known value to avoid undefined behavior.
  • Another participant highlights the lack of error checking for the return value of fscanf, raising concerns about potential issues with end-of-file, file errors, or non-integer inputs.
  • A further suggestion is made to explicitly initialize the variables 'num' and 'total' to 0, noting that while they are global variables and may default to 0, this practice is not reliable if they were declared locally.

Areas of Agreement / Disagreement

Participants generally agree on the presence of bugs in the code and the need for improvements, but there is no consensus on the best approach to correct these issues.

Contextual Notes

Participants have not resolved how to implement the suggested improvements or the implications of the identified bugs on the program's functionality.

sh4rif
Messages
4
Reaction score
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
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.
 
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?
 
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
 
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.
 

Similar threads

Replies
1
Views
2K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 6 ·
Replies
6
Views
3K
  • · Replies 16 ·
Replies
16
Views
9K
  • · Replies 11 ·
Replies
11
Views
2K
Replies
1
Views
14K
  • · Replies 54 ·
2
Replies
54
Views
5K
  • · Replies 2 ·
Replies
2
Views
11K
  • · Replies 3 ·
Replies
3
Views
5K