How do I correct this loop?

It doesn't go back to the beginning........

Page 1 of 1

9 Replies - 949 Views - Last Post: 24 May 2008 - 05:02 AM Rate Topic: -----

#1 kyeong  Icon User is offline

  • New D.I.C Head

Reputation: -3
  • View blog
  • Posts: 32
  • Joined: 12-February 08

How do I correct this loop?

Post icon  Posted 23 May 2008 - 10:24 PM

Please ignore the problems below, I think I solved the problems, but when I compiled it, another problem occurred.

This is the error I get; parse error before ' ( ' token. On line 32.

I googled and searched here to see if anyone had similar problem and couldn't find an answer. I have typed all the semi-colons where they are supposed to be and the brackets seem to be correct also. So, what's up? Do I have to add a new "#include?"

Thanks.


Never mind, I found out that it was the semi-colon, but now there is another problem. They are listed below.

Problem 1
After compiling it and executing the program. Let say I enter 3 numbers in a series, along with the terminated character, of numbers and then hit enter; but for some reason, right after I hit enter, "Enter number:" appears 3 times then prints out all the mean, sum, etc.

Problem 2
In the end, whenever I type in "y" to continue, it goes in an never ending loop instead of asking "Enter number:" back in the beginning. I looked at my professor's notes and did the same thing for this loop. I made sure the brackets are correct and etc, and can't figure out why it isn't going back to the beginning.[/size]

Thanks.

#include <stdio.h>
#include <math.h>

int main(void)
	{	

	double number = 0; 
	int count = 0;
	char answer;
	double max = 0;
	double min = 9999;
	double sum = 0;
	double vsum = 0;
	double mean = 0;
	double vari = 0;
	double devi = 0;
				
		do {
				
			
			  do 
				{
					printf("Enter numbers: \n");
					scanf("%lg",&number);	
				}	
				
					while (number > -1)
						{
				
							count++;
	
							if (number > max)
							max = number;
			
							else if (number < min)
							min = number;
							
							sum += number;
							vsum += number * number;	
			
						}

			mean = sum / count;
			vari = (vsum / count) - (mean * mean);
			devi = sqrt(vari);
			
			printf("You entered %i numbers \n", count);
			printf("Maximum value entered: %g \n", max);
			printf("Minimum value entered: %g \n", min);
			printf("Sum of all the values read: %g \n", sum);
			printf("Mean of all values: %g \n", mean);
			printf("Variance of all values: %g \n", vari);
			printf("Standard Deviation of all values: %g \n", devi);
	
			printf("\nWould you like to do it again (Y/N)? ");
			scanf(" %c",&answer);
			} while (answer!='N');

		return 0;
	}




This post has been edited by kyeong: 23 May 2008 - 11:43 PM


Is This A Good Question/Topic? 0
  • +

Replies To: How do I correct this loop?

#2 corliss  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 4
  • View blog
  • Posts: 198
  • Joined: 25-October 06

Re: How do I correct this loop?

Posted 23 May 2008 - 10:48 PM

Hi kyeong,
I would replace the do while statments with switches. This way you will be able to check to bunk entrys... such as andything other than Y and N.
This will also let you use a more fitting control structure.
do while = not so good.
Cheers,
p.s. If you need more help ask
Was This Post Helpful? 0
  • +
  • -

#3 kyeong  Icon User is offline

  • New D.I.C Head

Reputation: -3
  • View blog
  • Posts: 32
  • Joined: 12-February 08

Re: How do I correct this loop?

Posted 23 May 2008 - 10:53 PM

I would love to try that, but we haven't gone over switches in class and I wouldn't want her to think that I was cheating on this assignment. We just started class couple weeks ago.

I'm working on it now and still can't solve the problem. Actually, I did solve problem two, but problem one still exists and I'm trying to see why the program isn't calculating all the stuff that I need it to.

This post has been edited by kyeong: 23 May 2008 - 10:53 PM

Was This Post Helpful? 0
  • +
  • -

#4 joske  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 43
  • View blog
  • Posts: 297
  • Joined: 04-September 07

Re: How do I correct this loop?

Posted 24 May 2008 - 01:42 AM

I reworked your second do/while loop a little, it was a little bit messed up: the count++ etc. stuff must be inside that do-loop.
I also placed the variable definitions inside the first do loop, so everything is reset when you press "Y" at the end of the program.

#include <stdio.h>
#include <math.h>

int main(void)
{   
    char answer;
    do 
    {
        double number = 0; 
        int count = 0;
        double max = 0;
        double min = 9999;
        double sum = 0;
        double vsum = 0;
        double mean = 0;
        double vari = 0;
        double devi = 0;

        do 
        {
            printf("Enter numbers: \n");
            scanf("%lg",&number);   

            if (number > -1)
            {
                count++;

                if (number > max)
                max = number;

                else if (number < min)
                min = number;

                sum += number;
                vsum += number * number;    

            }
        } while (number > -1);

        mean = sum / count;
        vari = (vsum / count) - (mean * mean);
        devi = sqrt(vari);

        printf("You entered %i numbers \n", count);
        printf("Maximum value entered: %g \n", max);
        printf("Minimum value entered: %g \n", min);
        printf("Sum of all the values read: %g \n", sum);
        printf("Mean of all values: %g \n", mean);
        printf("Variance of all values: %g \n", vari);
        printf("Standard Deviation of all values: %g \n", devi);

        printf("\nWould you like to do it again (Y/N)? ");
        scanf(" %c",&answer);
    } while (answer != 'N' && answer != 'n');

    return 0;
}


This post has been edited by joske: 24 May 2008 - 01:47 AM

Was This Post Helpful? 0
  • +
  • -

#5 skater_00  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 12
  • View blog
  • Posts: 256
  • Joined: 30-April 08

Re: How do I correct this loop?

Posted 24 May 2008 - 03:53 AM

Declaring (eventually initializing) variables looks cleaner and requires less typing when you use multiple variables of the same datatype.

double number(0), max(0), min(9999), sum(0), vsum(0), mean(0), vari(0), devi(0);
int count(0);
char answer;


Was This Post Helpful? 0
  • +
  • -

#6 kyeong  Icon User is offline

  • New D.I.C Head

Reputation: -3
  • View blog
  • Posts: 32
  • Joined: 12-February 08

Re: How do I correct this loop?

Posted 24 May 2008 - 04:45 AM

Thank you everyone, now I understand what the problem was, but I still have this one little problem that I still can't figure out why it does it.

After entering, lets say 4 numbers, enter numbers appears 4 times before the summary prints out. How come? This just seems strange because I can't see what problem could be causing this.

Thanks.
Was This Post Helpful? 0
  • +
  • -

#7 gabehabe  Icon User is offline

  • GabehabeSwamp
  • member icon




Reputation: 1430
  • View blog
  • Posts: 11,006
  • Joined: 06-February 08

Re: How do I correct this loop?

Posted 24 May 2008 - 04:49 AM

It sounds like you're outputting them in a loop. Can I see your final code?
Was This Post Helpful? 0
  • +
  • -

#8 kyeong  Icon User is offline

  • New D.I.C Head

Reputation: -3
  • View blog
  • Posts: 32
  • Joined: 12-February 08

Re: How do I correct this loop?

Posted 24 May 2008 - 04:50 AM

Here it is.

#include <stdio.h>
#include <math.h>

int main(void)
{   
    char answer;
    do 
    {
        double number = 0; 
        int count = 0;
        double max = 0;
        double min = 9999;
        double sum = 0;
        double vsum = 0;
        double mean = 0;
        double vari = 0;
        double devi = 0;

        do 
        {
            printf("Enter numbers: \n");
            scanf("%lg",&number);   

            if (number > -1)
            {
                count++;

                if (number > max)
                max = number;

                if (number < min)
                min = number;

                sum += number;
                vsum += number * number;    

            }
        } while (number > -1);

        mean = sum / count;
        vari = (vsum / count) - (mean * mean);
        devi = sqrt(vari);

        printf("You entered %i numbers \n", count);
        printf("Maximum value entered: %g \n", max);
        printf("Minimum value entered: %g \n", min);
        printf("Sum of all the values read: %g \n", sum);
        printf("Mean of all values: %g \n", mean);
        printf("Variance of all values: %g \n", vari);
        printf("Standard Deviation of all values: %g \n", devi);

        printf("\nWould you like to do it again (Y/N)? ");
        scanf(" %c",&answer);
    } while (answer != 'N' && answer != 'n');

    return 0;
}



This post has been edited by kyeong: 24 May 2008 - 04:51 AM

Was This Post Helpful? 0
  • +
  • -

#9 gabehabe  Icon User is offline

  • GabehabeSwamp
  • member icon




Reputation: 1430
  • View blog
  • Posts: 11,006
  • Joined: 06-February 08

Re: How do I correct this loop?

Posted 24 May 2008 - 04:59 AM

Just move printf("Enter numbers: \n"); out of your loop. You might also want to suggest that the user can enter a negative number to quit, or suggest that the numbers can be seperated by a space or a new line (return key)
Here is your code:
#include <stdio.h>
#include <math.h>

int main(void)
{
    char answer;
    do
    {
        double number = 0;
        int count = 0;
        double max = 0;
        double min = 9999;
        double sum = 0;
        double vsum = 0;
        double mean = 0;
        double vari = 0;
        double devi = 0;

    // MOVE THE PROMPT OUT OF THE LOOP
    printf("Enter numbers, each seperated by hitting return or space: \n");
        do
        {
            scanf("%lg",&number);

            if (number > -1)
            {
                count++;

                if (number > max)
                max = number;

                if (number < min)
                min = number;

                sum += number;
                vsum += number * number;

            }
        } while (number > -1);

        mean = sum / count;
        vari = (vsum / count) - (mean * mean);
        devi = sqrt(vari);

        printf("You entered %i numbers \n", count);
        printf("Maximum value entered: %g \n", max);
        printf("Minimum value entered: %g \n", min);
        printf("Sum of all the values read: %g \n", sum);
        printf("Mean of all values: %g \n", mean);
        printf("Variance of all values: %g \n", vari);
        printf("Standard Deviation of all values: %g \n", devi);

        printf("\nWould you like to do it again (Y/N)? ");
        scanf(" %c",&answer);
    } while (answer != 'N' && answer != 'n');

    return 0;
}

Hope this helps :)

This post has been edited by gabehabe: 24 May 2008 - 05:04 AM

Was This Post Helpful? 0
  • +
  • -

#10 kyeong  Icon User is offline

  • New D.I.C Head

Reputation: -3
  • View blog
  • Posts: 32
  • Joined: 12-February 08

Re: How do I correct this loop?

Posted 24 May 2008 - 05:02 AM

Wow, thanks.

I was moving both the prompt and scanf out of the loop together. I didn't even know you could do that.

Thanks!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1