954,492 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

odd even count from FILE

HI, I'm writing a code for the very first time, and I'm completely stuck. Some guidance would be great.


I'm writing a C program that reads integars from file "integars.dat". The program should show how many numbers you have entered, how many among them were even, and how many were odd. Also the average of integer numbers that were odd. It should be done using nested loops, ifs whiles fors, as its a beginners program.

It does read in the integars from the FILE, and counts them properly , except it doesnt count the odd and even integars.

#include <stdio.h>
int
 
 
main (void)
 
 
{
    FILE *in;
    int number, s, count, w, j, status, sum, n;
    double y, average;
 
 
    in = fopen ("integars.dat", "r");
 
 
    count = 0; 
    sum = 0;
    average = y;
 
 
 
    status = fscanf (in, "%d", &number);
    while (status != EOF)
        
    {
        status = fscanf(in, "%d", &s);
        for (j=1; j<=s; j++)
 
        {
            count = count + 1;
            fscanf (in, "%lf", &y);
            average = (double)sum/number;
 
         }
  
              fscanf(in, "%d", &w);
              if (n%2==0){
              printf ("There are %d even numbers.\n", w);
 
          }
          else
          printf ("There are %d odd numbern.\n", w);
     }
 
   status = fscanf (in, "%d", &number);
  
 
fclose (in);
 
 
   printf ("There are %d numbers in the file.\n", count);
 
    return (0);
}


"integars.dat"
3
7
2
11
4

neveragn
Newbie Poster
16 posts since Oct 2011
Reputation Points: 10
Solved Threads: 0
 

[boilerplate_help_info]

Posing requests for help must be well thought out if you want help quickly and correctly.  Your post did not meet the criteria for quality help. You may get some posts, but are they going to be useful?  Check your post with these checkpoints - what is it <em>you</em> missed:<ul><li>Ask a question that can be answered. Do not ask
- What's wrong with my code?
- Why doesn't this work?
- Anything else that does not give us useful information</li>
<li>Post your code.  If we don't know what you did, how can we possibly help?
- Use <strong>PROPER FORMATTING</strong> --  <a href="http://www.gidnetwork.com/b-38.html">see this</a> 
- Use CODE Tags so your formatting is preserved.
If we can't follow your code, it's difficult to help. We don't care that you're still working on it. If you want us to read it, it must be readable</li>
<li>Explain what the code is supposed to do.  If we don't know where the target is, how can we help you hit it?</li>
<li>Explain what actually happened! If we don't know where the arrow went when you shot it, how can we tell what went wrong and how far from the target you are?</li>
<li>If you have errors, post them! We can't see your screen.  We can't read your mind. You need to tell us what happened.</li>
<li>To <strong>not</strong> ask for code. We are not a coding service. We will help you fix your code. 
    If anyone posts working code for you, they are a cheater. 
    If you use that code <em>you</em> are a cheater.</li>
<li>Do <strong>not</strong> bore us with how new you are. We can tell by your code.
- Do not apologize. We were all new, and unless you are completely 
  brain dead you will get better.
- Do not ask us to "take it easy on you."
- Do not say "I don't know what's going on." That's obvious since
  you posted for help. Use that time wisely by <strong>explaining</strong> as best 
  you can so we can help.</li>
<li><strong>Do not post your requirements and nothing else. </strong>We view that as a lazy do-nothing student that wants us to do their work for them. That's cheating and we <em>will</em> be hard on you.</li>
<li><strong>Do not tell us how urgent it is.</strong> Seriously, for us there is no urgency at all. Many that can help will ignore any URGENT or ASAP requests.</li>
</ul>Think more about your next post so we don't have to play 20 questions to get the info we need to help you.


[/boilerplate_help_info]

WaltP
Posting Sage w/ dash of thyme
Moderator
10,505 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
 

I don't see the need for nested loops here, just keep a running count:

while (fscanf(in, "%d", &number) == 1) {
    ++n;

    if (number % 2 == 0)
        ++even;
    else {
        sum += number;
        ++odd;
    }
}

/* Calculate the average... */

/* Display the results */
Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

I dont need a code, I just need help in solving my own. I have reached this far, and it's my very first time, all i need is someone to guide me.

neveragn
Newbie Poster
16 posts since Oct 2011
Reputation Points: 10
Solved Threads: 0
 

I don't see the need for nested loops here, just keep a running count:

while (fscanf(in, "%d", &number) == 1) {
    ++n;

    if (number % 2 == 0)
        ++even;
    else {
        sum += number;
        ++odd;
    }
}

/* Calculate the average... */

/* Display the results */


I tried this way, but then it gives me error when I printf the count for integars in the FILE

neveragn
Newbie Poster
16 posts since Oct 2011
Reputation Points: 10
Solved Threads: 0
 
I tried this way, but then it gives me error when I printf the count for integars in the FILE


Then your call to printf() was wrong, not the loop for building metrics. :icon_rolleyes:

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 
Then your call to printf() was wrong, not the loop for building metrics. :icon_rolleyes:

Is this right?

count = count  + 1


but before giving printf for even integars

neveragn
Newbie Poster
16 posts since Oct 2011
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: