Jump to content

C++ program. How to call separate function in the main()


Tampitump
 Share

Recommended Posts

I'm doing my last programming assignment. We have to make a 1-D array for wind speed, a 1-D array for temperature, and a 2-D array for wind chill (set to 0). Then we have to take the formula for calculating wind chill with the given temps and wind speeds and calculate the wind chill. Then we have to output the results in a table onto the screen. The instructions specify that we have to do our calculations in a separate function from the main, however when I try to do that it never works. I don't know how to call the separate function in the main. I copied and pasted the body of the separate function into the main (as you'll see below) and it works. Please tell me how to call the separate function in the main so I can meet the requirements.

 

Here's my code:

 

#include <iostream>
#include <cmath>
#include <math.h>
#include <vector>
#include <array>
using namespace std;
int temp[6] = {0};
int wind[7] = {0};
int windChill[7][6] = {0};
int main()
{
int temp[6] = {40,30,20,10,0,-10};
int wind [7] = {5,10,15,20,25,30,35};
int windChill [7][6] = {0};
for (int i = 0; i < 7; i++)
{
for (int j = 0; j < 6; j++)
{
windChill [j] = 35.74 + 0.6215 * temp[j] - 35.75 * pow(wind, 0.16) + 0.4275 * temp[j] * pow(wind, 0.16);
windChill [j] = windChill [j] + 0.5;
}
}
cout <<" "<<temp[0]<<" "<<temp[1]<<" "<<temp[2]<<" "<<temp[3]<<" "<<temp[4]<<" "<<temp[5]<<"\n";
for (int i =0;i<7; i++)
{
cout<<wind<<" "<<windChill[0]<<" "<<windChill[1]<<" "<<windChill[2]<<" "<<windChill[3]<<" "<<windChill[4]<<" "<<windChill[5]<<"\n";
}
int c;
cin>>c;
return 0;
}
int calc()
{
for (int i = 0; i < 7; i++)
{
for (int j = 0; i < 6; i++)
{
windChill [j] = 35.74 + 0.6215 * temp[j] - 35.75 * pow(wind, 0.16) + 0.4275 * temp[j] * pow(wind, 0.16);
windChill [j] = windChill [j] + 0.5;
}
return 0;
}
cout <<" "<<temp[1]<<" "<<temp[2]<<" "<<temp[3]<<" "<<temp[4]<<" "<<temp[5]<<" "<<temp[6]<<"\n";
for (int i =0;i<7; i++)
{
cout<<wind<<" "<<windChill[1]<<" "<<windChill[2]<<" "<<windChill[3]<<" "<<windChill[4]<<" "<<windChill[5]<<" "<<windChill[6]<<"\n";
}
}
Thanks!
Link to comment
Share on other sites

You never passed arguments to function?

http://www.cplusplus.com/doc/tutorial/functions/

The problem with your code is that windChill etc. variables used in calc() are defined in main(), but they're accessible only from main() body (local variables on CPU stack).

You have to pass them as arguments to calc(),

so calc() will be able to see them and use.

 

int calc()
{
for (int i = 0; i < 7; i++)
{
for (int j = 0; i < 6; i++)
{

windChill [j] = 35.74 + 0.6215 * temp[j] - 35.75 * pow(wind, 0.16) + 0.4275 * temp[j] * pow(wind, 0.16);
windChill [j] = windChill [j] + 0.5;
}
return 0;
}

Here you probably have error also,
return 0 in the middle of loop? What for?

ps. Are not you using indent style for every bracket? Or it was truncated by forum?

It would show you that return is in the middle of for() loop.

 

ps2. If program compiles fine, but does not work as expected, you have (at least) three choices:

1) manually go through entire source code analyzing in your brain what CPU is doing and when

2) use debugger built-in Visual Studio and enter to body of function.. line by line you will see which line is executed.. preview memory and variables, you will see how they are changing with time.

3) print debug info from code. f.e. you have for() loop, output counter, and it'll show 0..1...2....3...4 and so on, print variables to stdio, so you will see what is happening..

Edited by Sensei
Link to comment
Share on other sites

Once again, when I take that second function and paste it into the main(), it works. All I need to do is call that function from the main so it will execute. How do I do that?


Our instructions say to do all of the calculations in a separate function from the main(). I have the main(), which contains the arrays. Then there is a separate function from the main() that does the calculations. When I post this other function into the main, the program runs and does what it needs to do. Now I just need to know how to get the main() function to call that other function and execute it without it being inside the main().

Edited by Tampitump
Link to comment
Share on other sites

I know Java, but not C++.

So, out of guess, I think you need to call your functions from main method by the following way.

Creating object of main class, initializing instant variables by constructor and then calling method through that object using the dot operator.

I do so in Java.....Not sure whether it works in C++.

Link to comment
Share on other sites

I know Java, but not C++.

So, out of guess, I think you need to call your functions from main method by the following way.

Creating object of main class, initializing instant variables by constructor and then calling method through that object using the dot operator.

I do so in Java.....Not sure whether it works in C++.

 

It's possible but plentiful more than required to finish this exercise.

He just needs to read link that I gave in previous post, and to want to learn instead of utilize the forum community..

Edited by Sensei
Link to comment
Share on other sites

This is what I was telling you at the beginning of the semester. Break the problems down.

 

A few things to think about:

1) What are the big picture steps? You're explicitly told to break the problem down into functions and call those functions in main. Your big steps are those functions. What are they?

2) Are your steps supposed to print something to console, or are they supposed to return a value for use in other functions?

3) What values (and what kind of values) do your steps need to take as inputs for them to work?

 

Your calc() function doesn't take any inputs, so it's not getting data from the rest of the program. You need to tell it to use the provided values.

 

So, you need:

int calc(input 1 datatype input 1 name, ..., input n datatype input n name)
A good rule of thumb for naming/breaking projects into parts is giving your functions the name of what it does. If you name a function 'calc', it should calculate something and only do that. If you want it to calculate AND display, the name should reflect that. Break your tasks up so the names are as short as they can be. 'calc', while not optimal (what are you calculating?) is better than 'calcanddisplay'. So put your cout code into a new function that takes the output of calc().

 

So you should have something like:

int calc(datatype input, datatype input 2)
{
magic happens;
return CalcOutput;
}

void display(datatype input)
{
std::cout << input << '\n';
}

int main()
{
calc(input, input 2);
display(CalcOutput);

return 0;
}
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.