Thread: Help with math program...

  1. #1
    Registered User
    Join Date
    Jan 2007
    Posts
    4

    Help with math program...

    For my C++ class we have to make a program that outputs a histogram based on 25 random numbers and show the average, mode, range, minimum number, and maximum number.
    The lesson we are currently in is vectors so therefore I have to use them.

    Here's what I have so far...

    Code:
    #include<a:mylib.h>
    #include<lvp\vector.h>
    
    
    
    void histogram()
    {
      vector<long>num((26));
      for(int y=1;y<num.length();y++){
      num[y]=1+random(50);
    
    
     }
     cout<<" 1-5: ";
     for(y=0;y<num.length(); y++){
     if(0<num[y] && num[y]<=5)
     cout<<"*";
      }cout<<endl;
      cout<<" 6-10: ";
      for(y=0;y<num.length();y++){
      if(6<=num[y] && num[y]<=11)
      cout<<"*";
       }cout<<endl;
       cout<<"11-15: ";
       for(y=0; y<num.length();y++){
       if(11<=num[y] && num[y]<=15)
       cout<<"*";
        }cout<<endl;
        cout<<"16-20: ";
        for(y=0; y<num.length();y++){
        if(16<=num[y] && num[y]<=20)
        cout<<"*";
         }cout<<endl;
         cout<<"21-25: ";
         for(y=0; y<num.length();y++){
         if(21<=num[y] && num[y]<=25)
        cout<<"*";
           }cout<<endl;
           cout<<"26-30: ";
           for(y=0;y<num.length();y++){
           if(26<=num[y] && num[y]<=30)
           cout<<"*";
    	}cout<<endl;
    	cout<<"31-35: ";
    	for(y=0; y<num.length();y++){
    	if(31<=num[y] && num[y]<=35)
    	cout<<"*";
    	 }cout<<endl;
    	 cout<<"36-40: ";
    	 for(y=0; y<num.length();y++){
    	 if(36<=num[y] && num[y]<=40)
    	 cout<<"*";
    	  }cout<<endl;
    	  cout<<"41-45: ";
    	  for(y=0; y<num.length();y++){
    	  if(41<=num[y] && num[y]<=45)
    	  cout<<"*";
    	   }cout<<endl;
    	   cout<<"46-50: ";
    	   for(y=0; y<num.length();y++){
    	   if(46<=num[y] && num[y]<=50)
    	   cout<<"*";
      }
    }
    
    void maxmin()
    {
    int y,z,x;
    
      for(y=0; y<50; y++){
      y=z;
    
       for(y=50; 0<y; y++){
       y=x;
    
    }
    
    
    void average()
    {
    int y,a;
    double sum;
      sum=0;
      for(y=0; y<num.length(); y++){
      sum+=num[y];
      a=sum/25;
     }
    }
    
    
    void mode()
    {
      int y;
      vector<long>num((51,0));
      for(y=0; y<num.length(); y++)
      cout<<(num[y])++;
    }
    
    
    
    void main()
    {
      int y,a,x,z;
      clrscr();
      randomize();
      cout<<num[y];
      histogram();
      maxmin();
      cout<<x;
      cout<<z;
      cout<<(z-x);
      average();
      cout<<a;
      mode();
    }
    }}
    I've been getting these errors:
    *Declaration Syntax at Top bracket in the average function
    *Declaration Missing ; at last bracket of the main block

    I've been trying everything and I still can't find what is up. Also I would like to know if I'm doing my program right so far. Also sorry for the program being kinda everywhere. When I try to fix things in a program the program usually ends up this way...

    Any help would be appreciated

  2. #2
    Lean Mean Coding Machine KONI's Avatar
    Join Date
    Mar 2007
    Location
    Luxembourg, Europe
    Posts
    444
    First, go ahead and decide what indent style you want to use and once you've decided, apply it STRICTLY.

    As soon as you got the indentation right, you'll find the problem by yourself.

  3. #3
    Sanity is for the weak! beene's Avatar
    Join Date
    Jul 2006
    Posts
    321
    use int main; instead of void main;
    this problem is only secondary, it will not fix your errors

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Code:
    void histogram()
    {
      vector<long>num((26));
      for(int y=1;y<num.length();y++){
      num[y]=1+random(50);
    
    
     }
     cout<<" 1-5: ";
     for(y=0;y<num.length(); y++){
     if(0<num[y] && num[y]<=5)
     cout<<"*";
      }cout<<endl;
      cout<<" 6-10: ";
      for(y=0;y<num.length();y++){
      if(6<=num[y] && num[y]<=11)
      cout<<"*";
       }cout<<endl;
       cout<<"11-15: ";
       for(y=0; y<num.length();y++){
       if(11<=num[y] && num[y]<=15)
       cout<<"*";
        }cout<<endl;
        cout<<"16-20: ";
        for(y=0; y<num.length();y++){
        if(16<=num[y] && num[y]<=20)
        cout<<"*";
         }cout<<endl;
         cout<<"21-25: ";
         for(y=0; y<num.length();y++){
         if(21<=num[y] && num[y]<=25)
        cout<<"*";
           }cout<<endl;
           cout<<"26-30: ";
           for(y=0;y<num.length();y++){
           if(26<=num[y] && num[y]<=30)
           cout<<"*";
    	}cout<<endl;
    	cout<<"31-35: ";
    	for(y=0; y<num.length();y++){
    	if(31<=num[y] && num[y]<=35)
    	cout<<"*";
    	 }cout<<endl;
    	 cout<<"36-40: ";
    	 for(y=0; y<num.length();y++){
    	 if(36<=num[y] && num[y]<=40)
    	 cout<<"*";
    	  }cout<<endl;
    	  cout<<"41-45: ";
    	  for(y=0; y<num.length();y++){
    	  if(41<=num[y] && num[y]<=45)
    	  cout<<"*";
    	   }cout<<endl;
    	   cout<<"46-50: ";
    	   for(y=0; y<num.length();y++){
    	   if(46<=num[y] && num[y]<=50)
    	   cout<<"*";
      }
    }
    Count the '{'s, and then count the '}'s. Oops.

    You have functions inside other functions.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  5. #5
    Registered User
    Join Date
    Jan 2007
    Posts
    4
    Alright thanks for the advice. I had some extra brackets.

    Anyways I've been having problems now with the actual math portion. My histogram is suppose to show 25 numbers yet it seems like it's in random. The min and max is completely wrong, and bunch of strange numbers show up for average and nothing shows for mode.

    Here's what I have now...

    Code:
    #include<a:mylib.h>
    #include<lvp\vector.h>
    
    
    void histogram()
    {
      vector<long>num((26));
      for(int y=1;y<num.length();y++){
      num[y]=1+random(50);
     cout<<" 1-5: ";
     for(y=0;y<num.length(); y++){
     if(0<num[y] && num[y]<=5)
     cout<<"*";
      }cout<<endl;
      cout<<" 6-10: ";
      for(y=0;y<num.length();y++){
      if(6<=num[y] && num[y]<=11)
      cout<<"*";
       }cout<<endl;
       cout<<"11-15: ";
       for(y=0; y<num.length();y++){
       if(11<=num[y] && num[y]<=15)
       cout<<"*";
        }cout<<endl;
        cout<<"16-20: ";
        for(y=0; y<num.length();y++){
        if(16<=num[y] && num[y]<=20)
        cout<<"*";
         }cout<<endl;
         cout<<"21-25: ";
         for(y=0; y<num.length();y++){
         if(21<=num[y] && num[y]<=25)
        cout<<"*";
           }cout<<endl;
           cout<<"26-30: ";
           for(y=0;y<num.length();y++){
           if(26<=num[y] && num[y]<=30)
           cout<<"*";
    	}cout<<endl;
    	cout<<"31-35: ";
    	for(y=0; y<num.length();y++){
    	if(31<=num[y] && num[y]<=35)
    	cout<<"*";
    	 }cout<<endl;
    	 cout<<"36-40: ";
    	 for(y=0; y<num.length();y++){
    	 if(36<=num[y] && num[y]<=40)
    	 cout<<"*";
    	  }cout<<endl;
    	  cout<<"41-45: ";
    	  for(y=0; y<num.length();y++){
    	  if(41<=num[y] && num[y]<=45)
    	  cout<<"*";
    	   }cout<<endl;
    	   cout<<"46-50: ";
    	   for(y=0; y<num.length();y++){
    	   if(46<=num[y] && num[y]<=50)
    	   cout<<"*";
      }
     }
    }
    
    void maxmin()
    {
    int y,z,x;
    
      for(y=0; y<50; y++){
      y=z;
      }
    
    
       for(y=50; 0<y; y++){
       y=x;
       }
    }
    
    
    void average()
    {
    int y;
    double sum;
      sum=0;
      vector<long>num((26));
      for(y=0; y<num.length(); y++){
      sum+=num[y];
      cout<<sum/25;
     }
    }
    
    
    void mode()
    {
      int y;
      vector<long>num((51,0));
      for(y=0; y<num.length(); y++){
      cout<<num[y];
     }
    }
    
    
    
    void main()
    {
      int a,x,z;
      clrscr();
      randomize();
      vector<long>num((26));
      for(int y=1;y<num.length();y++){
      num[y]=1+random(50);
      }
      cout<<"Histogram:"<<endl;
      histogram();
      cout<<endl;
      maxmin();
      cout<<endl;
      cout<<"max: "<<x<<endl;
      cout<<"min: "<<z<<endl;
      cout<<"range: "<<(x-z)<<endl;
      average();
      //cout<<"average: "<<a<<endl;
      cout<<"mode: ";
      mode();
    }
    Can anyone help??

  6. #6
    Lean Mean Coding Machine KONI's Avatar
    Join Date
    Mar 2007
    Location
    Luxembourg, Europe
    Posts
    444
    No. Seriously, you can't expect anyone to read that code. It's like reading a book that has been printed all in one line. Format it correctly and then try again.

  7. #7
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    Consier using whitespace, break that code into more readable peices.
    Double Helix STL

  8. #8
    Registered User
    Join Date
    Apr 2007
    Posts
    27

    Ouch

    Ok... I think what you're doing (it is VERY difficult to read) is having every for loop go through every number?

    It seems like you check every single value every single step of the way. Build a struct called vector, and a function called vectorPlacement.

    Objective:
    -generate your random numbers then have it walk through the array of vectors and place them in the right spot


    My personal algorithm to this:
    Let's say we use '\n' and ' ' characters.

    -create vector struct with parameters of newlineValue and spaceValue
    -input randomValue
    -calculate newlineValue and spaceValue off of randomValue
    (this would be some ugly operation (if I did it) in which you would modulus (&#37 the number by the allowed number of spaces per (new) line, let's say ten, take the remainder, this remainder is the number of spaces, this would then be subtracted off of whatever the randomValue was, which would be divided by ten (now that it is evenly divisible), and this would be the number in newlineValue.)


    -sort first by newlineValue, then by spaceValue (at the same time, so it goes (0,0)(1,2)(1,3)(2,5)(4,6)(4,8), if that makes sense (pulling numbers out of my "arse"))

    -Now you put them into some function vectorRelativity or something (comparison) in which it, now they are in order, calculates the difference between them, and stores THOSE values. (0,0)(1,2)(0,1)(1,5)(2,6)(0,2) (REMEMBER that if doing this way, that space characters AFTER a newline character do NOT need to be compared since you want all the spaces so they are in the correct spot.)

    -take array of values and put them into vectorPlacement, in which it is now going to print something along the lines of (if using old values):

    '\n'
    ' '
    ' '
    '*'
    ' '
    '*'
    '\n'
    ' '
    ' '
    ' '
    ' '
    ' '
    '*'
    ......ect

    The idea being that those would line up in some fashion. This might give you a goofy looking graph the way I put it, it would probably be tilted in my fashion, but it gives you an algorithm template to work from. Not neccessarily the best, but it's what I could think of off the top of my head.
    Last edited by blurrymadness; 04-20-2007 at 12:04 AM.

  9. #9
    Registered User
    Join Date
    Apr 2007
    Posts
    26
    If it was me I would completely change your math code. I would put all 25 numbers into an array. Put them through a sorting algorithm(I would probaly use bubble sort, look on this site for a tutorial). Then take Numbers[0] for the min Numbers[24] for the max and Numbers[12] for the median. As for the average I would do a for loop to add all th values in the array and devide by 25. As for the drawing of the vectors I'll leave it up to you since that seems to be the homework part of it and you seem to be able to do that.

  10. #10
    Registered User
    Join Date
    Apr 2004
    Posts
    173
    Code:
    #include <iostream>
    #include <vector>
    #include <string>
    #include <algorithm>
    
    using namespace std;
    
    void print(const vector<int>& vec) {
    	int table[26];
    	memset(table,0,sizeof(table));
    	for (int i = 0; i < vec.size(); i++) {
    		table[(vec[i]-1)/5]++;
    	}
    	for (int i = 0; i < 26; i++) {
    		cout << i*5+1 << "-" << i*5+5 << ": ";
    		cout << string(table[i],'*') << "\n";
    	}
    }
    
    double avg(const vector<int>& vec) {
    	double res = 0.0;
    	for (int i = 0; i < vec.size(); i++) res += vec[i];
    	return res/vec.size();
    }
    
    double maxval(const vector<int>& vec) {
    	return vec.at(vec.size()-1);
    }
    
    double minval(const vector<int>& vec) {
    	return vec.at(0);
    }
    
    int mode(const vector<int>& vec) {
    	if (vec.size() <= 0) return -1;
    	int best = 0, num = vec[0]; int last = vec[0]; int cur = 1;
    	for (int i = 1; i < vec.size(); i++) {
    		if (vec[i] == last) { cur++; }
    		else {
    			if (cur > best) {
    				num = last;
    				best = cur;
    			}
    			cur = 0;
    		}
                                    last = vec[i];
    	}
    	return last;
    }
    
    int main()
    {
    	int num; vector<int> vec;
    	while (cin >> num) vec.push_back(num);
    	sort(vec.begin(),vec.end());
    	print(vec);
    	cout << "Max: " << maxval(vec) << "\n";
    	cout << "Min: " << minval(vec) << "\n";
    	cout << "Avg: " << avg(vec) << "\n";
    	cout << "Mode: " << mode(vec) << "\n";
    	return 0;
    }
    Do some simple maths to simplify the code. Here's a sample implementation. You still need to tweak it into your specification and finish what is missing - but it nevertheless gives you an idea on how to simplify the code. Also it doesn't use random numbers but just input - but that is easily modifiable. For the mode, I take the smallest mode (i.e. the smallest number that has the highest frequency) - if you want to return "all" the numbers with the same mode, you can just iterate once through to find the maximum mode and then iterate again to leech all the ones with that mode number and return it as a vector.
    Last edited by 0rion; 04-23-2007 at 04:27 AM.
    The cost of software maintenance increases with the square of the programmer's creativity.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Math program help.
    By Deadlyaim in forum C++ Programming
    Replies: 2
    Last Post: 09-29-2006, 07:57 PM
  2. Can someome help me with a program please?
    By WinterInChicago in forum C++ Programming
    Replies: 3
    Last Post: 09-21-2006, 10:58 PM
  3. I need some help with my program please.
    By agentxx04 in forum C Programming
    Replies: 9
    Last Post: 09-26-2004, 07:51 AM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM