Quantcast

Maximum PC

It is currently Sat Apr 19, 2014 6:45 pm

All times are UTC - 8 hours




Post new topic Reply to topic  [ 24 posts ]  Go to page 1, 2  Next
Author Message
 Post subject: C++ help. Program to calculate sum then determine average
PostPosted: Fri May 08, 2009 8:58 pm 
8086
8086

Joined: Sat Dec 27, 2008 7:17 pm
Posts: 28
All, I am "trying" to write a program to where the user inputs a series of numbers and the program will determine the average of those numbers. I thought I was doing pretty well, but now I think I am way off track. We must use an array to hold the numbers and use a function (sum) -- to calculate the sum of the numbers in the array.

#include <iostream>
using namespace std ;
int a[5];
float avg(int a[]);
float x;
int sum = sum[a];

int main ()

{
for
(int i=0; i<5; i++)
sum = sum + a[];


{
cout << "Please enter an integer: ";
cin >> a[i];
}

x = avg(a);

cout <<"The average of your numbers is: "<< x << endl;

return 0; //end with success!
}



This is what I have so far. Please let me know why it is not working if you have any idea. I think I am on the right track but some things are out of order. Thanks!


Top
  Profile  
 
 Post subject:
PostPosted: Fri May 08, 2009 9:35 pm 
Million Club - 5 Plus*
Million Club - 5 Plus*
User avatar

Joined: Sun Sep 12, 2004 6:37 pm
Posts: 4740
Location: In the monkey's litterbox
1. In the future, please use the code tags to make the code more readable.

2. Even though you can get away with not using braces on loops and ifs, always use them. (hint: they're causing a problem in your code)

3. I don't think that all of your variables should be globally scoped, so I would move them inside of int main().

I would start by making a version that just reads in the numbers and exits (maybe printing the list out to the console). Then add the averaging function.


Top
  Profile  
 
 Post subject:
PostPosted: Sat May 09, 2009 8:02 pm 
Willamette
Willamette
User avatar

Joined: Fri Jul 06, 2007 9:29 am
Posts: 1447
What's the pseudocode behind your function? You might want to try walking through it in order to figure out the order of your statements. It'll jump out at you if you do.


Top
  Profile  
 
 Post subject:
PostPosted: Sun May 10, 2009 3:28 pm 
8086
8086

Joined: Sun Jan 06, 2008 2:40 pm
Posts: 37
Location: Massachusetts
I like the approach those above me have taken to helping you with your programming problems. These hints will work wonders on you way to mastering languages.

Furthering with jcollins's point - don't think in syntax, think in pseudo code. What do you want ur function to do? Grab a piece of paper, and write out the inputs, what you want the outputs to do, and most importantly: how you would do this WITHOUT a computer. (I know this approach doesn't work in all cases, but it works exceptionally well with these small algorithms).

Next - use a developement enviornment that has line tabbing. I am not sure if you are using line tabbing since it wouldn't come through that way with a Copy/Paste, but use the spacing to help you stay organized. Think of them as { and its closer } as "Blocks". You have a For Block.

Yea, check the for block and see what its doing.

A specific syntactical point:
Functions are called like foo(parameters) not foo[parameters]. Check you function calls.

You are on the right track. Just use pseudo code and that piece of paper to think through your algorithm. I teach programming, and that is what i have found to help the best.


Top
  Profile  
 
 Post subject:
PostPosted: Sun May 10, 2009 6:03 pm 
SON OF A GUN
SON OF A GUN
User avatar

Joined: Mon Nov 01, 2004 5:41 am
Posts: 11605
jcollins wrote:
What's the pseudocode behind your function? You might want to try walking through it in order to figure out the order of your statements. It'll jump out at you if you do.


Pseudocode makes babies cry.


Top
  Profile  
 
 Post subject:
PostPosted: Sun May 10, 2009 7:22 pm 
Willamette
Willamette
User avatar

Joined: Fri Jul 06, 2007 9:29 am
Posts: 1447
lol, well, to be honest, I agree with him. I don't really use pseudocode much anymore. I also don't usually use pseudocode in the way "they" like (ie. build the code around it). If I pseudocode, I usually do a very very sketchy outline of what I want to do to get the algorithm correct, then code. Sort of pseudocode for pseudocode. :)

I see it really as a benefit for the beginning programmers. Once you've got your logic and the language down, and a set of experience behind you, you don't need to worry as much about pseudocoding it.


Top
  Profile  
 
 Post subject:
PostPosted: Mon May 11, 2009 4:19 am 
SON OF A GUN
SON OF A GUN
User avatar

Joined: Mon Nov 01, 2004 5:41 am
Posts: 11605
Maybe if I have time I will write an example of this in C#. Should be sufficiently different to prevent copy and paste :)


Top
  Profile  
 
 Post subject:
PostPosted: Mon May 11, 2009 4:23 am 
Java Junkie
Java Junkie
User avatar

Joined: Mon Jun 14, 2004 10:23 am
Posts: 24218
Location: Granite Heaven
I like his point but he leaves out the obvious. He says "I don't use pseudocode because I think about my code in code". This is a great point and entirely valid .. for an experienced programmer.

For someone who is new to programming, however, using pseudocode allows you to think about code in familiar terms. Until you learn to "think" in code, pseudocode will help you think in terms of 'best practices' and properly structured code.


Top
  Profile  
 
 Post subject:
PostPosted: Mon May 11, 2009 4:50 am 
SON OF A GUN
SON OF A GUN
User avatar

Joined: Mon Nov 01, 2004 5:41 am
Posts: 11605
I wasn't even a huge fan of it as a learning tool... but I was mostly self taught and by the time I entered school where stuff like that would be used, i was already thinking (dreaming?) in code.


Top
  Profile  
 
 Post subject:
PostPosted: Mon May 11, 2009 11:50 am 
Bitchin' Fast 3D Z8000
Bitchin' Fast 3D Z8000
User avatar

Joined: Mon Jun 14, 2004 4:04 pm
Posts: 985
Location: Earth
jcollins wrote:
lol, well, to be honest, I agree with him. I don't really use pseudocode much anymore. I also don't usually use pseudocode in the way "they" like (ie. build the code around it). If I pseudocode, I usually do a very very sketchy outline of what I want to do to get the algorithm correct, then code. Sort of pseudocode for pseudocode. :)

I see it really as a benefit for the beginning programmers. Once you've got your logic and the language down, and a set of experience behind you, you don't need to worry as much about pseudocoding it.


Pseudocode was meant to help developers sketch out the logic without going into too much detail about the code. I'm a professional developer and I still use pseudocode. It has nothing to do with levels, its use is for making things easier to read. If I have time to 'sketch out' my methods, I do that too, but often times, writing out the logic in pseudocode makes sense.


Top
  Profile  
 
 Post subject:
PostPosted: Mon May 11, 2009 11:53 am 
Bitchin' Fast 3D Z8000
Bitchin' Fast 3D Z8000
User avatar

Joined: Mon Jun 14, 2004 4:04 pm
Posts: 985
Location: Earth
Jipstyle wrote:
I like his point but he leaves out the obvious. He says "I don't use pseudocode because I think about my code in code". This is a great point and entirely valid .. for an experienced programmer.

For someone who is new to programming, however, using pseudocode allows you to think about code in familiar terms. Until you learn to "think" in code, pseudocode will help you think in terms of 'best practices' and properly structured code.


Pseudocode was not meant to divide pros from amateurs. I think people overlook pseudocode's use in the real world and chalk it to 'pros' versus 'joes'.

I think and breathe in C# but pseudocode helps me verbally understand what a method is supposed to do. Pseudocode is always very helpful when talking to Business Analysts - especially when I'm helping them design functional requirements.

When all else fails, I also use my own version of UML. :P


Top
  Profile  
 
 Post subject:
PostPosted: Mon May 11, 2009 6:30 pm 
Willamette
Willamette
User avatar

Joined: Fri Jul 06, 2007 9:29 am
Posts: 1447
DJSPIN80 wrote:
Pseudocode is always very helpful when talking to Business Analysts - especially when I'm helping them design functional requirements.


Too true, especially when they're new to a project and have no clue as to what programming is. It's almost like tossing one of those life savers to someone that's drowning. Of course, on some complex projects, it can be more like walking up to a baby seal with a club too... :)


Top
  Profile  
 
 Post subject:
PostPosted: Tue May 12, 2009 5:13 am 
Java Junkie
Java Junkie
User avatar

Joined: Mon Jun 14, 2004 10:23 am
Posts: 24218
Location: Granite Heaven
I don't show BAs pseudocode ... it confuses the little darlings.

I show them the code as a blackbox ... X comes in and Y comes out.


Usually, I can dumb it down enough for them to understand.

Ok, not usually. But I try.


Top
  Profile  
 
 Post subject:
PostPosted: Tue May 12, 2009 8:17 am 
Bitchin' Fast 3D Z8000
Bitchin' Fast 3D Z8000
User avatar

Joined: Mon Jun 14, 2004 4:04 pm
Posts: 985
Location: Earth
Jipstyle wrote:
I don't show BAs pseudocode ... it confuses the little darlings.

I show them the code as a blackbox ... X comes in and Y comes out.


Usually, I can dumb it down enough for them to understand.

Ok, not usually. But I try.


Depends, a lot of the BA's I've dealt with were former programmers. They hated working as one, so they became BA's instead. I had one BA who was so good, if she weren't my BA for a project, I protested. :P

I used pseudocode that made sense to people, so it wasn't true to developers pseudocode. However, when discussing things with other devs, you bet your bottom dollar I pseudocode like a monster.

I think as devs, we often dismiss these simple, time tested techniques because we want to look cool. I know in a lot of projects I've worked in, pseudocode was never used but they sure as heck had a lot of UML diagrams and other diagrams that had boxes connecting other things. I think it confused a lot of devs, which is probably why the project was an awful failure at times. I'm a devoted fan of simplicity; less is more.


Top
  Profile  
 
 Post subject:
PostPosted: Tue May 12, 2009 10:04 am 
SON OF A GUN
SON OF A GUN
User avatar

Joined: Mon Nov 01, 2004 5:41 am
Posts: 11605
Okay, enough derailing. @ OP:

Here is my C# implementation and a sample run:
Quote:
Enter a series of numbers (one at a time). Use 'x' to calculate the average:
Enter first number: 2
Enter next number: 4
Enter next number: 6
Enter next number: 87
Enter next number: 4
Enter next number: 24
Enter next number: 5
Enter next number: 2
Enter next number: 34
Enter next number: 7
Enter next number: 56
Enter next number: 8
Enter next number: 4
Enter next number: 25
Enter next number: x
You entered 14 numbers.
The sum is: 268
The avg is: 19
(press any key to continue)


Code:
using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string input = string.Empty;
            int num = 0;
            int count = 0;
            List<int> numbers = new List<int>();
            int sum = 0;
            int avg = 0;

            Console.WriteLine("Enter a series of numbers (one at a time). Use 'x' to calculate the average:");
            Console.Write("Enter first number: ");
            input = Console.ReadLine();
            while (input.ToLower() != "x")
            {
                count++;
                if (Int32.TryParse(input, out num))
                {
                    numbers.Add(num);
                }

                Console.Write("Enter next number: ");
                input = Console.ReadLine();
            }

            sum = GetSum(numbers);
            avg = GetAvg(count, sum);

            Console.WriteLine("You entered " + count + " numbers.");
            Console.WriteLine("The sum is: " + sum);
            Console.WriteLine("The avg is: " + avg);
            Console.WriteLine("(press any key to continue)");
            Console.ReadKey();
        }

        private static int GetAvg(int count, int sum)
        {
            return (sum / count);
        }

        private static int GetSum(List<int> numbers)
        {
            int sum = 0;

            foreach (int val in numbers)
            {
                sum += val;
            }

            return sum;
        }
    }
}


Not bad for 7 minutes :)


Top
  Profile  
 
 Post subject:
PostPosted: Tue May 12, 2009 3:42 pm 
Willamette
Willamette
User avatar

Joined: Fri Jul 06, 2007 9:29 am
Posts: 1447
Won't your average function die if there are no numbers?


Top
  Profile  
 
 Post subject:
PostPosted: Tue May 12, 2009 4:23 pm 
Bitchin' Fast 3D Z8000
Bitchin' Fast 3D Z8000
User avatar

Joined: Mon Jun 14, 2004 4:04 pm
Posts: 985
Location: Earth
jcollins wrote:
Won't your average function die if there are no numbers?


Sure will. If you enter 'x' as the input, it breaks out of the loop and runs the GetAvg() method. It should have:


Code:
public static int GetAvg(int count, int sum)
{
   if (count > 0) return (sum / count);
   else return 0;
}


Top
  Profile  
 
 Post subject:
PostPosted: Tue May 12, 2009 6:04 pm 
SON OF A GUN
SON OF A GUN
User avatar

Joined: Mon Nov 01, 2004 5:41 am
Posts: 11605
jcollins wrote:
Won't your average function die if there are no numbers?


You want me to do error checking too?

Jebus...

I spoz I could have spent 10 minutes on it, but I was at work. :)


Top
  Profile  
 
 Post subject:
PostPosted: Wed May 13, 2009 4:36 am 
Willamette
Willamette
User avatar

Joined: Fri Jul 06, 2007 9:29 am
Posts: 1447
But of course. :P Don't want the noobies to get the wrong idea.


Top
  Profile  
 
 Post subject:
PostPosted: Wed May 13, 2009 4:38 am 
SON OF A GUN
SON OF A GUN
User avatar

Joined: Mon Nov 01, 2004 5:41 am
Posts: 11605
jcollins wrote:
But of course. :P Don't want the noobies to get the wrong idea.
They should be learning that in school. I probably wrote WAY too much as it is lol, but the program was really simple.


Top
  Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 24 posts ]  Go to page 1, 2  Next

All times are UTC - 8 hours


Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group