Visual C++

Moderators: Lundin
Number of threads: 379
Number of posts: 695

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Finding the lowest score Posted by loryricky on 5 Oct 2008 at 7:39 PM
Again I don't know if I am calling it correctly.
Can someone help me please!

Thank you!

#include <iostream>
using namespace std;

int getNumAccidents ();
void findLowest (int,int,int,int,int);

int main()


{
int acc1, acc2, acc3, acc4, acc5;

cout << "Enter The Number Of Automobile Accidents Reported A Year \n";
cout << "By Region And I Will Show you The Region With The Fewest Accidents\n";
cout << "North\n";
cin >> acc1;
cout << "South\n";
cin >> acc2;
cout << "East\n";
cin >> acc3;
cout << "West\n";
cin >> acc4;
cout << "Central\n";
cin >> acc5;

getNumAccidents ();
{
int acc1,acc2,acc3,acc4,acc5;



if (acc1, acc2, acc3, acc4, acc5 < 0)
cout << "Accidents Must Be Equal or Greater Than 0\n";
}

cout<< " The Region With The Least Accidents Is: " << endl;

findLowest(acc1, acc2, acc3, acc4, acc5);

return 0;
}
int acc1,acc2,acc3,acc4,acc5;
void findLowest(int,int,int,int,int)

{
if (acc1 < acc2,acc3,acc4,acc5)

{
cout << "North Is The Region With The Least Accidents With" << acc1 << endl;
}

if (acc2 < acc1,acc3,acc4,acc5)
{
cout << "South Is The Region With The Least Accidents With" << acc2 << endl;
}

if (acc3 < acc1,acc2,acc4,acc5)
{
cout << "East Is The Region With The Least Accidents With" << acc3 << endl;
}

if (acc4 < acc1,acc2,acc3,acc5)
{
cout << "West Is The Region With The Least Accidents With" << acc4 << endl;
}

if (acc5 < acc1,acc2,acc3,acc4)
{
cout << "Central Is The Region With The Least Accidents With" << acc5 << endl;
}

else
{
cout << "There Are More Than One Region With The Same Ammount Of Accidents" << endl;
}
}




Report
Re: Finding the lowest score Posted by Lundin on 6 Oct 2008 at 12:02 AM
First of all, the functions must be defined outside main(). Second, the function parameters are copies of the ones in main(), so they don't necessarily need to have the same name, but they need to have some kind of name. For convenience, I picked the same names in the example below, but you could as well have named them a1, a2, a3 etc.

Also, you compare the variables in the wrong way. An if-statement contains no built-in intelligence at all, you have to compare all variables one by one. Meaning that will be a long tiresome code... it is much easier to do this when you know how to handle arrays.

You should have something like this:


void findLowest (int acc1, int acc2, int acc3, int acc4, int acc5);

int main()
{
  int acc1, acc2, acc3, acc4, acc5;
  ...
  findLowest(acc1, acc2, acc3, acc4, acc5);
  ...
}

void findLowest (int acc1, int acc2, int acc3, int acc4, int acc5)
{
  if(acc1 < acc2 && acc1 < acc3 && acc1 < acc4 && acc1 < acc5)
  {
    cout << ...
  }
  else if(acc2 < acc1 && ....)
  {
  }

}
Report
Re: Finding the lowest score Posted by loryricky on 6 Oct 2008 at 7:31 AM
: First of all, the functions must be defined outside main(). Second,
: the function parameters are copies of the ones in main(), so they
: don't necessarily need to have the same name, but they need to have
: some kind of name. For convenience, I picked the same names in the
: example below, but you could as well have named them a1, a2, a3 etc.
:
: Also, you compare the variables in the wrong way. An if-statement
: contains no built-in intelligence at all, you have to compare all
: variables one by one. Meaning that will be a long tiresome code...
: it is much easier to do this when you know how to handle arrays.
:
: You should have something like this:
:
:
:
: void findLowest (int acc1, int acc2, int acc3, int acc4, int acc5);
: 
: int main()
: {
:   int acc1, acc2, acc3, acc4, acc5;
:   ...
:   findLowest(acc1, acc2, acc3, acc4, acc5);
:   ...
: }
: 
: void findLowest (int acc1, int acc2, int acc3, int acc4, int acc5)
: {
:   if(acc1 < acc2 && acc1 < acc3 && acc1 < acc4 && acc1 < acc5)
:   {
:     cout << ...
:   }
:   else if(acc2 < acc1 && ....)
:   {
:   }
: 
: }
:



So, what happens with the getNum? I am supposed to use that to get the number of accidents and then output with the findLowest...
Report
Re: Finding the lowest score Posted by Lundin on 7 Oct 2008 at 1:28 AM
I just showed how to use functions in C, but it should be something similar.

If/when you know how to use arrays and pointers, that code will be so much smoother. It will then be something like this:

int findLowest (int* acc, int size)
{
  int lowest;  
  int i;
  
  lowest = acc[0];

  for(i=1; i<size; i++)
  {
    if(acc[i] < lowest)
    {
      lowest = acc[i];
    }
  }

  return lowest;
}



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.