12 Replies - 620 Views - Last Post: 19 December 2015 - 02:51 AM Rate Topic: -----

#1 yikietngu  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 18-December 15

how to return the values in a if selection of a function?

Posted 18 December 2015 - 06:42 PM

Hi, I am the beginner in C++, the question ask us to find the number of high, medium, and low temperature , this is the code

#include<iostream>
#include<iomanip>
#include<fstream>
using namespace std;

int readFile(ifstream&, double []);
int computeC(double [], int);
double average(double [], int);
int grade(double [], int);
void writeFile(int);

int main ()
{
	ifstream ifile;
	ofstream ofile;
	const int size=10;
	double fah[size];
	int sizefah;
	int H=0, M=0, L=0;
	
	ifile.open("Fahrenheit.txt");
	if (ifile.fail())
		cout << "Error opening the file!" << endl;
		
	else 
	{
	
		sizefah=readFile(ifile, fah);
		ifile.close();
	}
	
	computeC(fah, sizefah);
	cout << "Average of the temperature: " << average(fah, sizefah) << endl; 
	grade(fah, sizefah);
	cout << "Number of high temperature: " << grade(fah,sizefah);
	cout << "Number of medium temperature: " <<	grade(fah, sizefah);
	cout << "Number of low temperature: " <<	grade(fah, sizefah);
	
	return 0;
}

int readFile(ifstream&inputF, double arr[])
{
	const int size=10;
	int count=0;
	int number;
	
	while(count<size && inputF>>arr[count])
		count ++;
		
	for(int i=0; i<count; i++)
	{
		cout << arr[i] << endl;
	}
	
	return count;
}

int computeC(double f[], int a)
{
	double C;

	
	
	for (int j=0; j<a; j++)
	{
		C=(5*(f[j]-32))/9;
		f[j]=C;
		cout << f[j] << endl;
	}	
	
	
	return a;	
}

double average(double c[], int B)/>
{
	double sum;
	double avg;
	
	for(int x=0; x<b; x++)
	{
		sum+=c[x];
	}
	
	avg=sum/b;
	return avg;
}

int grade(double cel[], int d)
{
	char grade;
	int numH=0, numM=0, numL=0;
	
	for(int i=0; i<d; i++)
	{
	
	if (cel[i]>=35)
		{
			grade='H';
			if(grade=='H')
			 numH++;
		}
		else if (cel[i]<35&&cel[i]>=20)
		{
			grade='M';
			if(grade=='M')
			numM++;
		}
		else if (cel[i]<20)
		{
			grade='L';
			if(grade=='L')
			numL++;
		}
			cout << cel[i] << " " << grade << endl;
	}
	
}


Is This A Good Question/Topic? 0
  • +

Replies To: how to return the values in a if selection of a function?

#2 Anarion  Icon User is offline

  • The Persian Coder
  • member icon

Reputation: 387
  • View blog
  • Posts: 1,663
  • Joined: 16-May 09

Re: how to return the values in a if selection of a function?

Posted 18 December 2015 - 08:13 PM

If you could specify more clearly what the problem is (also where it is), we would be able to help you faster and more accurate. Based on the topic's title, I see that you are wondering how to return the value from inside an if-statement? But then, I am not sure of what part of the source code you are talking about.

Reviewing your code, you could refactor the following function (not related directly to the question you had, but it improves your code's readability!):
int computeC(double f[], int a)
{
    double C;

    for (int j=0; j<a; j++)
    {
        C=(5*(f[j]-32))/9;
        f[j]=C;
        cout << f[j] << endl;
    }
    return a;
}

In the above function, the return value is completely unnecessary,. You are passing a to the function as it's second argument, and then at the end you are returning this value. This is redundant and you can get rid of it.
Also, variable C is redundant. After these modifications:
void computeC(double f[], int a)
{
    for (int j=0; j<a; j++)
    {
        f[j] = (5*(f[j]-32))/9;
        cout << f[j] << endl;
    }
}

Now, this function is converting the given array's values from Fahrenheit to Centigrade. If you need to preserve the Fahrenheit values, then you need to create another array of the same size inside this function and return it as a result of the conversion.

As a general design guideline, I would suggest to have the following in mind for most functions (until you get familiar with classes):

Quote

Each function should take an input (or more), process it, and then return the result as output.

Whenever possible, try to keep input and output as separate entities and do not mix them together. This can help you avoid problems/confusions later on.
Was This Post Helpful? 1
  • +
  • -

#3 yikietngu  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 18-December 15

Re: how to return the values in a if selection of a function?

Posted 18 December 2015 - 08:21 PM

Sorry, my problem is from the this

int grade(double cel[], int d)
091
{
092
    char grade;
093
    int numH=0, numM=0, numL=0;
094
     
095
    for(int i=0; i<d; i++)
096
    {
097
     
098
    if (cel[i]>=35)
099
        {
100
            grade='H';
101
            if(grade=='H')
102
             numH++;
103
        }
104
        else if (cel[i]<35&&cel[i]>=20)
105
        {
106
            grade='M';
107
            if(grade=='M')
108
            numM++;
109
        }
110
        else if (cel[i]<20)
111
        {
112
            grade='L';
113
            if(grade=='L')
114
            numL++;
115
        }
116
            cout << cel[i] << " " << grade << endl;
117
    }
118
     
119
}


want return the value of numH, numM, and numL to main funtion line 35 for numH, line 36 for numM and line 37 for numL

int main ()
013
{
014
    ifstream ifile;
015
    ofstream ofile;
016
    const int size=10;
017
    double fah[size];
018
    int sizefah;
019
    int H=0, M=0, L=0;
020
     
021
    ifile.open("Fahrenheit.txt");
022
    if (ifile.fail())
023
        cout << "Error opening the file!" << endl;
024
         
025
    else
026
    {
027
     
028
        sizefah=readFile(ifile, fah);
029
        ifile.close();
030
    }
031
     
032
    computeC(fah, sizefah);
033
    cout << "Average of the temperature: " << average(fah, sizefah) << endl;
034
    grade(fah, sizefah);
035
    cout << "Number of high temperature: " << grade(fah,sizefah);
036
    cout << "Number of medium temperature: " << grade(fah, sizefah);
037
    cout << "Number of low temperature: " <<    grade(fah, sizefah);
038
     
039
    return 0;
040
}

This post has been edited by snoopy11: 18 December 2015 - 11:25 PM
Reason for edit:: added [ CODE ] tags !

Was This Post Helpful? 0
  • +
  • -

#4 snoopy11  Icon User is offline

  • Engineering ● Software
  • member icon

Reputation: 1377
  • View blog
  • Posts: 4,318
  • Joined: 20-March 10

Re: how to return the values in a if selection of a function?

Posted 19 December 2015 - 12:07 AM

Well,

there are two ways you could do this,

use a struct

struct temperatures
{
    int numL=0;
    int numM=0;
    int numH=0;
};




and use it in a program like so.

#include <iostream>
#include <vector>
using namespace std;
struct temperatures
{
    int numL=0;
    int numM=0;
    int numH=0;
};

temperatures grade(vector<int> celsius)
{
    temperatures results;
    for(unsigned int i=0; i<celsius.size(); i++)
    {
        if (celsius[i]>=35)
        {
            results.numH++;
        }
        else if (celsius[i]<35 && celsius[i]>=20)
        {
            results.numM++;
        }
        else if (celsius[i]<20)
        {
            results.numL++;
        }


    }

    return results;
}
int main()
{
    vector<int> myTemperatures;

    myTemperatures.push_back(18);
    myTemperatures.push_back(21);
    myTemperatures.push_back(7);
    myTemperatures.push_back(13);
    myTemperatures.push_back(38);
    myTemperatures.push_back(25);
    myTemperatures.push_back(37);
    myTemperatures.push_back(3);

    temperatures myResults;

    myResults = grade(myTemperatures);

    cout << "Amount of high readings " << myResults.numH;
    cout << "\nAmount of medium readings " << myResults.numM;
    cout << "\nAmount of low readings " << myResults.numL;
    return 0;
}




or use a pointer

int grade(int cel[], int d, int* numH, int* numM, int* numL)
{
}




and use it in a program like so.

grade(fah,sizefah, &numH, &numM, &numL);


but I would go with the struct its far neater.

Best Wishes

Snoopy.
Was This Post Helpful? 0
  • +
  • -

#5 David W  Icon User is offline

  • DIC supporter
  • member icon

Reputation: 298
  • View blog
  • Posts: 1,839
  • Joined: 20-September 08

Re: how to return the values in a if selection of a function?

Posted 19 December 2015 - 01:06 AM

Hi again Snoopy,

I see you suggest C++11 (or greater) coding style for struct


Quote

struct temperatures
{
    int numL=0;
    int numM=0;
    int numH=0;
};



But then you go back to C coding style to pass by ref... ?

Quote

int grade(int cel[], int d, int* numH, int* numM, int* numL)
{
}

This post has been edited by David W: 19 December 2015 - 01:14 AM

Was This Post Helpful? 0
  • +
  • -

#6 snoopy11  Icon User is offline

  • Engineering ● Software
  • member icon

Reputation: 1377
  • View blog
  • Posts: 4,318
  • Joined: 20-March 10

Re: how to return the values in a if selection of a function?

Posted 19 December 2015 - 01:14 AM

Hi David,

No I did say I would recommend using the struct idea.

but gave the passing of the address idea as an alternative.

In this case I think it would be neater to use the struct.

Regards

Snoopy.
Was This Post Helpful? 0
  • +
  • -

#7 David W  Icon User is offline

  • DIC supporter
  • member icon

Reputation: 298
  • View blog
  • Posts: 1,839
  • Joined: 20-September 08

Re: how to return the values in a if selection of a function?

Posted 19 December 2015 - 01:28 AM

Hi again Snoopy,

I see now ... that I did not portray very well .. what I was getting at here?

I agree with using a struct to pass multiple values ...

but the comparison (here) in C++ ...

should it not be between passing back a struct holding the 3 values,

and,

passing the three values by reference ... (and not comparing it, to passing by C style pointers?)

Best regards,
David

This post has been edited by David W: 19 December 2015 - 01:31 AM

Was This Post Helpful? 0
  • +
  • -

#8 snoopy11  Icon User is offline

  • Engineering ● Software
  • member icon

Reputation: 1377
  • View blog
  • Posts: 4,318
  • Joined: 20-March 10

Re: how to return the values in a if selection of a function?

Posted 19 December 2015 - 01:32 AM

Hi David,

No I don't really see what you are getting at.

Also I am passing the values by address not reference.

Perhaps you could explain further ?

Regards

Snoopy.
Was This Post Helpful? 0
  • +
  • -

#9 David W  Icon User is offline

  • DIC supporter
  • member icon

Reputation: 298
  • View blog
  • Posts: 1,839
  • Joined: 20-September 08

Re: how to return the values in a if selection of a function?

Posted 19 December 2015 - 01:44 AM

Hi Snoopy,

Yes ... by address ... by C style taking of an address ... i.e. passing pointers.

But ... in C++ we usually prefer to pass by reference ... (and not pass C style pointers where C++ reference passing would normally be used.)

All that I was trying to 'tease you a bit about' was that ...

after using a C++11 (or greater example) ...

you then reverted to a C style of coding to make the comparison.

Does that help?

Best regards,
David
Was This Post Helpful? 0
  • +
  • -

#10 snoopy11  Icon User is offline

  • Engineering ● Software
  • member icon

Reputation: 1377
  • View blog
  • Posts: 4,318
  • Joined: 20-March 10

Re: how to return the values in a if selection of a function?

Posted 19 December 2015 - 02:07 AM

Well I should have probably used smart pointers if thats what you mean

but smart pointers would have muddied the waters more than was strictly necessary.

program that uses smart pointers and the struct idea.

#include <iostream>
#include <vector>
#include <memory>
using namespace std;
struct temperatures
{
    int numL=0;
    int numM=0;
    int numH=0;
};

temperatures grade(vector<int> celsius, std::shared_ptr<int> numH, std::shared_ptr<int> numM, std::shared_ptr<int> numL)
{
    temperatures results;

    for(unsigned int i=0; i<celsius.size(); i++)
    {
        if (celsius[i]>=35)
        {
            results.numH++;
            *numH= *numH+1;
        }
        else if (celsius[i]<35 && celsius[i]>=20)
        {
            results.numM++;
            *numM = *numM+1;
        }
        else if (celsius[i]<20)
        {
            results.numL++;
            *numL= *numL+1;
        }


    }

    return results;
}
int main()
{
    vector<int> myTemperatures;
    std::shared_ptr<int> H(new int(0));
    std::shared_ptr<int> M(new int(0));
    std::shared_ptr<int> L(new int(0));

    myTemperatures.push_back(18);
    myTemperatures.push_back(21);
    myTemperatures.push_back(7);
    myTemperatures.push_back(13);
    myTemperatures.push_back(38);
    myTemperatures.push_back(25);
    myTemperatures.push_back(37);
    myTemperatures.push_back(3);

    temperatures myResults;

    myResults = grade(myTemperatures, H, M, L);

    cout << "Amount of high readings " << myResults.numH;
    cout << "\nAmount of medium readings " << myResults.numM;
    cout << "\nAmount of low readings " << myResults.numL;

    cout << "\nAmount of high readings " << *H;
    cout << "\nAmount of medium readings " << *M;
    cout << "\nAmount of low readings " << *L;
    return 0;
}




see what I mean way more complicated now than was strictly necessary :) !

Best

Wishes

Snoopy.
Was This Post Helpful? 0
  • +
  • -

#11 yikietngu  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 18-December 15

Re: how to return the values in a if selection of a function?

Posted 19 December 2015 - 02:14 AM

sorry for asking. what is meant by

myTemperatures.push_back(18);
39
myTemperatures.push_back(21);
40
myTemperatures.push_back(7);
41
myTemperatures.push_back(13);
42
myTemperatures.push_back(38);
43
myTemperatures.push_back(25);
44
myTemperatures.push_back(37);
45
myTemperatures.push_back(3);
Was This Post Helpful? 0
  • +
  • -

#12 snoopy11  Icon User is offline

  • Engineering ● Software
  • member icon

Reputation: 1377
  • View blog
  • Posts: 4,318
  • Joined: 20-March 10

Re: how to return the values in a if selection of a function?

Posted 19 December 2015 - 02:37 AM

No its ok to ask,

Im using a vector and push_back is basically loading the value into the vector.


This is a learning site so don't feel afraid to ask questions you are supposed to ask questions :)


Regards

Snoopy.
Was This Post Helpful? 1
  • +
  • -

#13 David W  Icon User is offline

  • DIC supporter
  • member icon

Reputation: 298
  • View blog
  • Posts: 1,839
  • Joined: 20-September 08

Re: how to return the values in a if selection of a function?

Posted 19 December 2015 - 02:51 AM

And ... recall Google is the programmers friend.

You can always do a web search on C++ vector or C++ vector push_back

http://en.cppreferen...ector/push_back
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1