Help with basic program

This is a discussion on Help with basic program within the C Programming forums, part of the General Programming Boards category; Hello, I am trying to write a program where you enter an undefined amount of numbers and when you type ...

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    4

    Help with basic program

    Hello,
    I am trying to write a program where you enter an undefined amount of numbers and when you type in -1 it will output the average of the numbers you have entered. I am having trouble figuring out how to keep storing a new number. I figure I need a variable for the amount of numbers entered, and one for each of the numbers entered but it has to be unlimited.
    Here is what i have so far and I don't know if I have the right idea. it doesnt work and when I run it I get runtime errors.
    Code:
    #include <stdio.h>
    #include <math.h>
    
    int main(void)
    {
    	int n,newnumber,average,count=0,total;
    	printf("enter numbers: \n");
    	while (n != -1, newnumber != -1)
    	{
    	scanf_s("%lf",&n);
    	scanf_s("%lf",&newnumber);
    	total=n+newnumber;
    	count++;
    	}
    	if (n==-1,newnumber==-1)
    	average=total/count;
    	printf("average is: %i",average);
    getchar();
    return 0;
    }
    Last edited by brettski900; 03-03-2011 at 10:00 AM.

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,162
    Instead of keeping track of each number entered, just keep track of a running sum:
    Code:
    sum = 0
    numbers_entered = 0
    loop until input number is -1
    {
      input number from user
      if number is not -1
      {
        add number to sum
        increment numbers_entered
      }
    }
    print sum divided by numbers_entered
    If you understand what you're doing, you're not learning anything.

  3. #3
    Registered User
    Join Date
    Mar 2011
    Posts
    4
    ah okay I new it would be something simple!

    thank you

  4. #4
    Registered User
    Join Date
    Mar 2011
    Posts
    4
    Hi,
    well I am stuck on one more problem. I have to read a text file numbers.txt and list how many even numbers there are in the file. I think I might be over complicating things again everything comes out fine except the for (even_test=0) says i need ; before ) i tried a lot of things but i think if I can get that line working my code would be fine.
    I would really appreciate any help,
    Thank you
    (the numbers.txt is just a plain list of random numbers no order)
    Code:
    #include <stdio.h>
    #include <math.h>
    #define FILENAME "numbers.txt"
    int main (void)
    {
    	/* Declare variables */
    	int file_numbers,number_even,num_data,even_test;
    	number_even = 0;
    	FILE *numbers;
    	/* read numbers.txt file */
    	numbers = fopen(FILENAME,"r");
    	while ((fscanf(numbers,"%lf",&file_numbers)) == 2)
    	{
    		num_data++;
    		if (num_data == 1)
    		{
    			even_test = file_numbers%2;
    		}
    			for (even_test; even_test=0; number_even++)
    		
    	
    	printf("the amount of even numbers are: %i",number_even);
    	}
            getchar();
    	getchar();
    	return 0;
    }
    Here is an update of what ive changed, still having problems hopefully im getting closer!

    Code:
    #include <stdio.h>
    #include <math.h>
    #define FILENAME "numbers.txt"
    int main (void)
    {
    	/* Declare variables */
    	int file_numbers,number_even,even_test;
    	number_even = 0;
    	FILE *numbers;
    	/* read numbers.txt file */
    	numbers = fopen(FILENAME,"r");
    	while ((fscanf(numbers,"%lf",&file_numbers)) == 2)
    	{
    			even_test = file_numbers%2;
    		
    			if (even_test = 0)
    			
    				number_even++;
    						
    			
    		
    	}
    	printf("the amount of even numbers are: %i",number_even);
    	getchar();
    	getchar();
    	return 0;
    		
    }
    Last edited by brettski900; 03-03-2011 at 01:45 PM. Reason: changed some things in code, not getting file_numbers corrupted syntax error, still working on it

  5. #5
    Captain - Lover of the C
    Join Date
    May 2005
    Posts
    341
    I'm not sure what your goal is with num_data. You're on the right track with the % operator, but for loops are designed for... looping. I believe you want something more like:
    Code:
    if(even_test)
    {
        // Increment the even number counter
    }
    No looping necessary. Also, your printf statement should be after the while loop if you want to display the total after reading the entire file.
    Don't quote me on that... ...seriously

  6. #6
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,438
    [QUOTE=brettski900;1007478]
    Code:
    if (even_test == 0)
    Add another = sign in that if check. A single = is for assigning, a double == is for testing equality. The way you had it, the if check always sets even_test to zero and never enters the body of the if statement.
    Last edited by anduril462; 03-03-2011 at 01:51 PM.

  7. #7
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,438
    Also, your fscanf is incorrect. First, you have to use %d for integers. Second, it returns the number of successful conversions. You are asking it to convert one thing, so the most it can return is 1. Change the while loop to something like:
    Code:
    while ((fscanf(numbers, "%d", &file_numbers)) == 1) {

  8. #8
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Code:
    while ((fscanf(numbers,"%lf",&file_numbers)) == 2)
      {
         num_data++;
         if (num_data == 1)
         {
           even_test = file_numbers%2;
          if (even_test = 0)
           number_even++;
           printf("the amount of even numbers are: %i",number_even);
       }
    }
    Couple of things ... you defind numbers as an int, but you are scanning for doubles...
    You should probably use ...
    Code:
    while (fscanf("%d%", &file_numbers))
    The loop wil exit the first time fscanf() fails to convert a value.

    Second your odd/even test can be simplified like this...
    Code:
    number_even +=  ((file_numbers & 1) == 0) ? 1 : 0;
    Lastly your results line should probably be outside the loop unless you want to see it counting each number in turn...

  9. #9
    Registered User
    Join Date
    Mar 2011
    Posts
    4
    Thank you guys so much i took your advice and i made the program work! here it is for future reference.
    Code:
    #include <stdio.h>
    #include <math.h>
    /* define the file */
    #define FILENAME "numbers.txt"
    int main (void)
    {
    	/* Declare variables */
    	int file_numbers,number_even,even_test;
    	/* set number_even to 0 */
    	number_even = 0;
    	/* FILE is set to numbers */
    	FILE *numbers;
    	/* read numbers.txt file */
    	numbers = fopen(FILENAME,"r");
    	/* Loop read data if the function is read it will have a value of 1 
    	   if it does not have a value of 1, end loop*/
    	while ((fscanf(numbers,"%d",&file_numbers)) == 1)
    	{
    			/* divide the file_numbers by 2 */
    			even_test = file_numbers%2;
    			/* if the remainder is 0 then the number is even! */
    			if (even_test == 0)
    			/* add 1 to number_even */
    			number_even++;						
    					
    	}
    	/* print the number_even which will display the amount of even numbers */
    	printf("the amount of even numbers are: %i",number_even);
    	getchar();
    	getchar();
    	return 0;
    		
    }
    Thanks again!
    Brett
    Last edited by brettski900; 03-03-2011 at 02:11 PM. Reason: added comments

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help needed (problem with basic C program)
    By EpicYuzer in forum C Programming
    Replies: 15
    Last Post: 11-11-2010, 04:38 PM
  2. Command Line argument help, basic program
    By Rollo in forum C Programming
    Replies: 3
    Last Post: 10-31-2010, 12:51 PM
  3. Replies: 29
    Last Post: 10-22-2009, 11:01 AM
  4. Replies: 2
    Last Post: 05-10-2002, 04:16 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21