Sign in with
Sign up | Sign in
Your question

Need help with C programming!

Last response: in Apps General Discussion
Share
July 5, 2012 8:24:31 PM

  1. /*
  2. program: proj4
  3.  
  4. Name: Jordan Ward
  5. Email: jw363912@ohio.edu
  6.  
  7. Description: This program reads from an input file and
  8. prints out the min, max, and average of the numbers.
  9. */
  10.  
  11. #include <stdio.h>
  12.  
  13. int main(void)
  14. {
  15. FILE *inp;
  16. FILE *out;
  17.  
  18. int count = 0;
  19. int sum = 0;
  20. int min;
  21. int max;
  22. int input_status;
  23.  
  24. inp = fopen("data.dat", "r");
  25. out = fopen("data.out", "w");
  26.  
  27. input_status = fscanf(inp, "%d", &min);
  28.  
  29. while(input_status == 10)
  30.  
  31. count++;
  32.  
  33. if(min > input_status)
  34. max = min;
  35. else if(min < input_status)
  36. max = input_status;
  37.  
  38. sum++;
  39.  
  40. [ Wrote 49 lines ]
  41.  
  42. bash-3.00$ !g
  43. gcc assign4.c
  44. bash-3.00$ gcc -Wall assign4.c
  45. assign4.c: In function 'main':
  46. assign4.c:49:1: warning: control reaches end of non-void function
  47. bash-3.00$ nano assign4.c
  48. GNU nano 2.2.4 File: assign4.c
  49.  
  50. int max;
  51. int input_status;
  52.  
  53. inp = fopen("data.dat", "r");
  54. out = fopen("data.out", "w");
  55.  
  56. input_status = fscanf(inp, "%d", &min);
  57.  
  58. while(input_status == 1)
  59.  
  60. count++;
  61.  
  62. (not sure what to put here read my question down low)
  63.  
  64. sum++;
  65.  
  66. input_status = fscanf(inp, "%d", &min);
  67.  
  68.  
  69. fprintf(out, "Minimum value = %d", min);
  70. fprintf(out, "Maximum value = %d", max);
  71. fprintf(out, "Average value = %4lf", (double) (min + max) / 2);
  72.  
  73. fclose(inp);
  74. fclose(out);
  75. }


Okay this is an assignment and I have to make a pointer called whatever i want but i need two pointers, one for data file and one for an output file. I am having trouble in the while loop, I don't understand what to put in the () for the argument and then I don't understand how to find the min and max of my data file. Thanks!

More about : programming

July 5, 2012 9:00:23 PM

oh sorry i dont know what happened i will post the code again
m
0
l
July 5, 2012 9:00:50 PM

  1. /*
  2. program: proj4
  3.  
  4. Name: Jordan Ward
  5. Email: jw363912@ohio.edu
  6.  
  7. Description: This program reads from an input file and
  8. prints out the min, max, and average of the numbers.
  9. */
  10.  
  11. #include <stdio.h>
  12.  
  13. int main(void)
  14. {
  15. FILE *inp;
  16. FILE *out;
  17.  
  18. int count = 0;
  19. int sum = 0;
  20. int min;
  21. int max;
  22. int input_status;
  23.  
  24. inp = fopen("data.dat", "r");
  25. out = fopen("data.out", "w");
  26.  
  27. input_status = fscanf(inp, "%d", &min);
  28.  
  29. while(input_status == 1)
  30.  
  31. count++;
  32.  
  33. sum++;
  34.  
  35. input_status = fscanf(inp, "%d", &min);
  36.  
  37.  
  38. fprintf(out, "Minimum value = %d", min);
  39. fprintf(out, "Maximum value = %d", max);
  40. fprintf(out, "Average value = %4lf", (double) (min + max) / 2);
  41.  
  42. fclose(inp);
  43. fclose(out);
  44. }
m
0
l
Related resources
July 5, 2012 9:13:22 PM

Someone please help I don't understand and my professor said I should be able to do this after we had class today but I don't know.
m
0
l
a b L Programming
July 6, 2012 3:18:45 PM

I'll try to help you. I've rewritten some of your code:

  1. #include <stdio.h>
  2.  
  3. int main()
  4. {
  5. FILE *inp;
  6. FILE *out;
  7. int count = 0;
  8. int sum = 0;
  9. int min = 65535; // Minimum and maximum values
  10. int max = -65536;
  11. int input; // Input buffer
  12. int input_status;
  13. inp = fopen("data.dat", "r" );
  14. out = fopen("data.out", "w" );
  15. do
  16. {
  17. input_status = fscanf(inp, "%d", &input);
  18. if (input_status == 1)
  19. {
  20. count++;
  21. sum += input;
  22. if (min > input)
  23. min = input;
  24. if (max < input)
  25. max = input;
  26. }
  27. }
  28. while(input_status == 1);
  29. fprintf(out, "Minimum value = %d\n", min);
  30. fprintf(out, "Maximum value = %d\n", max);
  31. fprintf(out, "Average value = %4lf\n", (double)sum / (double)count);
  32. fclose(inp);
  33. fclose(out);
  34. }


Changed the formula for Average value, changed loop to
  1. do ... while
, added a variable for input buffer, set starting minimum and maximum values. Just ask if something does not work.

Here's the data I've tested it with.

data.dat
  1. 5 10 -100 15 30 60 80 1 0


data.out
  1. Minimum value = -100
  2. Maximum value = 80
  3. Average value = 11.222222
m
0
l
!