I have two functions. One is supposed to fill in an array, and the other takes that new array, finds the lowest number in that array, and prints the subscript.

int getNumAccidents(int acc[], const int size); Fills array num_acc[]
int findLowest(int acc[], const int size); Takes num_acc[], finds lowest value and returns the subscript.

Ultimately the goal for this program is for the first function to pass the name of a borough, and capture the amount of accidents in that borough for 2010. I have an array initialized for this already.

Then the second function will take those values and find the borough with the lowest value and print it's subscript.

For now I will be happy understanding how to properly return values from a function to use in another function in Main().

I feel like I am confusing myself slightly here and would appreciate any help you can provide. Thank you in advance.

#include <cstdlib>
#include <iostream>
#include <string>

using namespace std;
int getNumAccidents(int acc[], const int size); 
int findLowest(int acc[], const int size);

int main()
{
    string borough_name[5] = {"Brooklyn", "Queens", "Bronx", "Manhattan", "Staten Island"}; //ultimately I have to run this array through getNumAccidents 
        

    int num_acc[5];                                                                           
    int lowest; 
    int input;
    
    
    for (int i=0;i<array_size;i++)        //Fills in num_acc with number of accidents in 2010.
    {
     getNumAccidents(num_acc, 5);
     input = getNumAccidents(num_acc, 5);
    }
     for (int l=0;l<array_size;l++)       //Finds lowest number within num_acc and prints subscript.
     {
       findLowest(input, 5);
       lowest=findLowest(input, 5);
       cout << "The lowest accident state is " << lowest;
     }

    system("PAUSE");
    return EXIT_SUCCESS;
}

int getNumAccidents(int acc[], const int size) //Records the number of accidents occured in 2010 into num_acc[]
{
    int num=0;
    int i=0;

        cout << "Enter the number of accidents that occured in 2010 \n";
        cin >> acc[i];
        if (acc[i]<0)
             {
               cout << "Please enter a number grater than 0 \n"; 
               acc[i]=0;
               cin >> acc[i];
               i++;
                if (acc[i]<0)
                {
                 cout << "You have entered an incorrect value 2 times, please run program again \n";
                }
             } 
             else if (acc[i]>=0)
             {
             cout << " has had "   << acc[i] << " accidents in 2010 \n";
             i++;
             }      
             num=acc[i];
               return num;  //Returns Number of accidents.
}      
    
int findLowest(int arr[],const int size) //Finds lowest value in num_acc[] and returns subscript.
{
    int lowest=0;
    for (int i=0;i<size;i++)
    {
        if (arr[i]<arr[i+1])
          lowest=i;
    }     
    return lowest;  //Should returns subscript of lowest value. 
}

Recommended Answers

All 12 Replies

Several things can go wrong in a program. Among them...

  1. Functions are incorrect.
  2. Function parameter passing is incorrect.
  3. Something is wrong in the calling function.

There are other things that can go wrong, but let's concentrate on these three. When something goes wrong, you want to narrow it down to one (or more) of these problems. How do you do that? Simplify things. Tackle one and only one thing at a time. Confirm it works. One way is to write a few test programs. Test the functions themselves first.

First one to test...

#include <cstdlib>
#include <iostream>
#include <string>
 
using namespace std;
int getNumAccidents(int acc[], const int size); 
 
int main()
{
    int num_acc[5];                                                                           
 
    getNumAccidents(num_acc, 5);

    // print it - make sure it's what you typed in.
    for(int i = 0; i < 5; i++)
    {
        cout << num_acc[i] << "\t";
    }
    
    system("PAUSE"); /* consider replacing this with cin.get() twice */
    return EXIT_SUCCESS;
}
 
int getNumAccidents(int acc[], const int size) //Records the number of accidents occured in 2010 into num_acc[]
{
    int num=0;
    int i=0;
 
        cout << "Enter the number of accidents that occured in 2010 \n";
        cin >> acc[i];
        if (acc[i]<0)
             {
               cout << "Please enter a number grater than 0 \n"; 
               acc[i]=0;
               cin >> acc[i];
               i++;
                if (acc[i]<0)
                {
                 cout << "You have entered an incorrect value 2 times, please run program again \n";
                }
             } 
             else if (acc[i]>=0)
             {
             cout << " has had "   << acc[i] << " accidents in 2010 \n";
             i++;
             }      
             num=acc[i];
               return num;  //Returns Number of accidents.
}

When this one works with everything, then move on and test the other function. When both work fine, go back to your main program and debug it.

As a side note, read this regarding system("PAUSE");

http://www.gidnetwork.com/b-61.html

You seem to be on the right track. I gave your code a quick look over and a few things caught my eye:

1) You use a variable called "array_size" in the for loops, but I don't see it defined beforehand.

2) Instead of using the magic number 5 throughout the program, why not define an integer constant and assign it the value 5.

3) Why do you call "findLowest(input, 5)" twice, on lines 26 AND 27? I think you could just delete line 26. After all, you are assigning its output to "lowest" anyhow.

4) Same for "getNumAccidents(num_acc, 5)": why call it twice (lines 21 and 22)?

I am taking your advice and trying one function at a time. But while I am at it I decided to include the string(borough) in the function. I might as well troubleshoot the function one time with all variables.

I think I am having trouble in two areas here. Since the for loop is not in the function, but rather the main() program, I'm confused on how to run the array through the function.
Also I have an Int function with a string array and int array, returning only the Int array.

At this point i'm getting a Windows ".exe has stopped working" error.

#include <cstdlib>
#include <iostream>
#include <string>

using namespace std;
int getNumAccidents(string borough[], int acc[]); 


int main()
{
    string borough_name[5] = {"Brooklyn", "Queens", "Bronx", "Manhattan", "Staten Island"};
    int num_acc[5];                                                                         
    const int array_size = 5;
    int lowest; 
    int input;
    
    
    for (int i=0;i<array_size;i++)                //Fills in num_acc with number of accidents in 2010.
    {                                             //Sends borough name.
     getNumAccidents(borough_name, num_acc);
    }
     
    cin.get();
    return EXIT_SUCCESS;
}

int getNumAccidents(string borough[], int acc[]) //Records the number of accidents occured in 2010 into num_acc[]
{
    
    int i;
    int num=0;
   

        cout << "Enter the number of accidents that occured in "<< borough[i] << " \n";
        cin >> acc[i];
        if (acc[i]<0)
             {
               cout << "Please enter a number grater than 0 \n"; 
               num=0;
               cin >> acc[i];
               i++;
                if (acc[i]<0)
                {
                 cout << "You have entered an incorrect value 2 times, please run program again \n";
                }
             } 
             else if (acc[i]>=0)
             {
             num=acc[i];
             cout << borough[i] <<" has had " << num << " accidents in 2010 \n";
             i++;
             }      
        
             return num;
}

Same comment as I offered in one of your previous threads. Stick some comments in there to explain what everything does and change the variable names to something more meaningful. What does "num" represent? What does the function return? What EXACTLY is the function supposed to do? "Exactly" is in all caps on purpose. As much specificity as possible is needed here. This means you explaining, in English, to a friend who is eager to help, but has no judgment at all. Pretend you have a really stupid employee who will follow your orders to the letter, but you have to explain everything in minute detail to him. Part of this explanation is telling him what this function returns. You have an "int" in the front of your getNumAccidents, so you want him to give you a number when he's done, but he's not sure what. He asked me, but I couldn't tell him. I also couldn't explain what "i" represented. I told him "i" was usually a loop counter, but he reported back that he looked for the words "for" and "while" in the function, but couldn't see any.

To complicate things, I asked him if he was sure this wasn't a void function. He confirmed that it returns an integer. That confused me because I looked at line 20 and didn't see the code doing anything with the return value.

Hopefully this analogy doesn't annoy you too much. The point is to get you to think like you have to think as a programmer must think. Get in the right frame of mind and the English language will turn itself into C++ almost on its own. I often actually talk to myself and pretend I'm different people giving and taking orders on a factory floor.

You added a mistake that you didn't have before. Line 30 in your last attempt. i was initialized originally. Now it's not. What happened?

Now that I look at my code I was definitely very vague. It was late when I posted this last night and I wasn't seeing straight.

I can appreciate the analogy and am definitely not annoyed. I understand how difficult it is to solve problems with limited information. I appreciate your patience.

I am required to write a program here that determines which of the 5 boroughs of NY have the fewest reported auto accidents in one year. The program should have two functions:

int getNumAccidents(string) - Is passed the name of a borough. Asks user input for number of accidents reported in that region in one year. Validates(greater or equal to zero) the input and returns the value. It should be called once per borough.

int findLowest(int arr[]) - Is passed the five accident totals as an array of ints. It determines which is smallest and returns the subscript for that array element.

The main() function declares two arrays, string with borough names and an int array which holds accident numbers per borough.

Main() calls getNumAccidents to fill in the int array and then calls findLowest to learn which borough had the fewest accidents. It should print out the accident numbers for each borough and then print out the borough and number with the fewest accidents.

I also though if these were void functions they would make more sense and I could figure them out. But they both need to return int's.

Here is my updated code with more comments. It seems a little sloppy, but I am still learning the formatting of this site. I hope this helps clarify the program.

I have not included the int findLowest(int arr[]) function in the code yet, because I want to debug it one funct at a time, as you mentioned previously.

Thank you always for your advice.

#include <cstdlib>
#include <iostream>
#include <string>

using namespace std;
int getNumAccidents(string borough[], int acc[]); 


int main()
{
    string borough_name[5] = {"Brooklyn", "Queens", "Bronx", "Manhattan", "Staten Island"}; //Array of boroughs.
    int num_acc[5];          //Array which will hold the number of accidents. It will then be passed to 
    const int array_size = 5;  // int findLowest function to determine the lowest  number of accidents in that array and display that. 
    int lowest; 
    int input;
    
    
    for (int i=0;i<array_size;i++)                //Loops through function that will display a borough,borough_name,
    {                                             //and fill in num_acc with the number of accidents in that borough.
     getNumAccidents(borough_name, num_acc);      //This function will return an Int which will be run through another function Int findLowest 
    }                                             //which will in turn find the lowest number of accidents by borough.
     
    cin.get();
    return EXIT_SUCCESS;
}

int getNumAccidents(string borough[], int acc[]) //Calls the borough name(string) and asks for the accident 
{                                                //# in that borough and stores in int acc[] array. 
    
    int i;         //Not sure if this is necessary at all, but when set to zero it was only returning Brooklyn borough, 
    int num=0;    //because Brooklyn is in location zero in the array.
   
        
        //Below fills in int acc[] array with number of accidents in "borough[]"
        cout << "Enter the number of accidents that occured in "<< borough[i] << " \n";  
        cin >> acc[i];
        if (acc[i]<0)    //Validation of accidents, less than zero not accepted. 
             {
               cout << "Please enter a number grater than 0 \n";              
               num=0;
               cin >> acc[i];
               i++;
                if (acc[i]<0)
                {
                 cout << "You have entered an incorrect value 2 times, please run program again \n";
                }
             } 
             else if (acc[i]>=0)
             {
             num=acc[i];
             cout << borough[i] <<" has had " << num << " accidents in 2010 \n";  //Displays the data(num of accidents
             i++;                                                                 //) that was recorded sucessfully. 
             }      
        
             return num;       //Returns the number of accidents the user inputs for the borough displayed. This return will 
                               //be put into the findLowest function that will return lowest number of accidents.  
}

>> int getNumAccidents(string) - Is passed the name of a borough. Asks user input for number of accidents reported in that region in one year. Validates(greater or equal to zero) the input and returns the value. It should be called once per borough.

Who wrote this spec? An instructor? Or you? It doesn't match your function

int getNumAccidents(string boroughName) // from the spec.

Your code...

int getNumAccidents(string borough[], int acc[])

Make it match...

int getNumAccidents(string boroughName)
{
    int numAccidents;
    cout << "Enter number of accidents for " << boroughName << " : ";
    cin >> numAccidents;
    return numAccidents;
}

Change as needed for validation.

The call would be this...

for (int i=0;i<array_size;i++)                //Loops through function that will display a borough,borough_name,
    {                                             //and fill in num_acc with the number of accidents in that borough.
       num_acc[i] = getNumAccidents(borough_name[i]);      //This function will return an Int which will be run through another function Int findLowest 
    }

This is from my instructor.

After breaking it down like you said, it's a lot simpler than I thought. I was over complicating it and not reading the function for what it is.

Here is my modified code that returns the num of accidents, but when I introduce the findLowest function it says "invalid conversion from int to int*".

I'm not sure if I should have a for loop inside of findLowest or utilize the for loop in the main.

#include <cstdlib>
#include <iostream>
#include <string>

using namespace std;
int getNumAccidents(string borough); 
int findLowest(int arr[],const int size);

int main()
{
    string borough_name[5] = {"Brooklyn", "Queens", "Bronx", "Manhattan", "Staten Island"}; //Array of boroughs.
    int num_acc[5];          //Array which will hold the number of accidents. It will then be passed to 
    const int array_size = 5;  // int findLowest function to determine the lowest number of accidents in that array and display that. 
    int lowest; 
    int input;
    
    
    for (int i=0;i<array_size;i++)                //Loops through function that will display a borough,borough_name,
    {                                             //and fill in num_acc with the number of accidents in that borough.
     num_acc[i]=getNumAccidents(borough_name[i]); 
     findLowest(num_acc[i], array_size);//This function will return an Int which will be run through another function Int findLowest 
    }  
     cout << findLowest;
                                                  //which will in turn find the lowest number of accidents by borough.
     
    cin.get();
    return EXIT_SUCCESS;
}

int getNumAccidents(string borough) //Calls the borough name(string) and asks for the accident 
{                                                //# in that borough and stores in int acc[] array. 
    
    int numAccidents;         
   
        
        //Below fills in int acc[] array with number of accidents in "borough[]"
        cout << "Enter the number of accidents that occured in "<< borough << ": \n";  
        cin >> numAccidents;
        if (numAccidents<0)    //Validation of accidents.
             {
               cout << "Please enter a number grater than 0 \n";              
               numAccidents=0;
               cin >> numAccidents;
                if (numAccidents<0)
                {
                 cout << "You have entered an incorrect value 2 times, please run program again \n";
                 cin.get();
                }
             } 
             else if (numAccidents>=0)
             {
             cout << borough <<" has had " << numAccidents << " accidents in 2010 \n";  //Displays the data(num of accidents                                                                //) that was recorded sucessfully. 
             }      
        
             return numAccidents;       //Returns the number of accidents 
}      
    
int findLowest(int arr[],const int size) //Finds lowest value in num_acc[] and returns subscript.
{
    int lowest=arr[i];         //Stores first value as lowest. 
        if (arr[i]<arr[i+1])   
        {
          lowest=arr[i];       
        }
        else     
    return lowest;        //Returns value in arr[i] as lowest. 
}

Can't think of any "hints" to give you and don't have time to write up a draft to "nudge you along", so I'll just have to provide the corrected lines 18 - 23. Sometimes giving someone a fish teaches them to fish better than hints anyway.

Lines 18 - 23...

for (int i=0;i<array_size;i++) //Loops through function that will display a borough,borough_name and fill in num_acc with the number of accidents in that borough.
    {                                             
        num_acc[i]=getNumAccidents(borough_name[i]); 
    } 

    lowest = findLowest(num_acc, array_size);
    cout << borough_name[lowest] << " had the fewest accidents.\n";

Now fix your findLowest function.

I have modified this function over and over and can't seem to return the index of lowest or even the lowest value.

int findLowest(int arr[],const int size) //Finds lowest value in num_acc[] and returns subscript.
{
    int low=arr[0];
    int index;

    for(int i=0;i<size;i++)
    {
        if (arr[i]<low)
          low=arr[i];
          index=i;     
    }      
    return index;      //Return the index of the lowest value.
}
int findLowest(int arr[],const int size) //Finds lowest value in num_acc[] and returns subscript.
{
    int low=arr[0];
    int index;
 
    for(int i=0;i<size;i++)
    {
        if (arr[i]<low)
          low=arr[i];
          index=i;     
    }      
    return index;      //Return the index of the lowest value.
}

Put some brackets around the code that is to be executed if the if-statement is true. It never hurts and often helps. Right now your code is this (brackets added by me).

int findLowest(int arr[],const int size) //Finds lowest value in num_acc[] and returns subscript.
{
    int low=arr[0];
    int index;
 
    for(int i=0;i<size;i++)
    {
        if (arr[i]<low)
        {
          low=arr[i];
        }

        index=i;     
    }      
    return index;      //Return the index of the lowest value.
}

That's how the compiler is going to interpret it. Is that what you want? If not, change the brackets so that they are the way you want things to be.

you miss the brackets around the if statement.

edit: oh crap, didnt see page 2. my mistake. VernonDozier explained it already.

You know, I had brackets up and took them down because I had seen some examples online without them. Figuring out to use the "index" variable to get the subscript took some time.

Thank you both for your input. It's always appreciated.

Here is the working function:

int findLowest(int arr[],const int size) //Finds lowest value in num_acc[] and returns subscript.
{
    int low=arr[0];
    int index;
    for(int i=0;i<size;i++)
    {
        if (arr[i]<low)
        {
          low=arr[i];
          index=i;
        }
       
    }      
     
    return index;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.