Write an interactive C++ program that prompts the user for ten (10) real numbers and calculate the sum, average, and prints the result. Hint: you may use an array to store the data.

Program requirements:
- The program must contain a class in which all the required private and public identifiers and the following functions must be defined.
- The program must contain three or more functions using parameters. These functions should read the data, calculate the sum, find the average, and print the results.

C++
#include "stdafx.h"
#include <iostream>
using namespace std;


// class with identifiers and functions
class real
{
private:
double n[10],s;                     // declarations
int i;
double avg;


public:
real()
{
s=0;
}


void read()


{
cout<<"Please Enter 10 Numbers"<<endl;
for (i=0;i<10;i++)
{


cout<<"You have Entered Number:";
cin>>n;
}
};


void print()
{


for (i=0;i<10;i++)
s=s+n;



avg=s/10;



cout<<"The Sum           ="<<s<<endl;
cout<<"The Average      ="<<avg<<endl;
}
};
int main()
{
real a;


a.read();


a.print();
system("PAUSE");
return 0;
}

Dani AI

Generated

This thread shows the right idea but a few common C++ pitfalls remain. attempted a class-based solution; spotted the key indexing fix. To meet the assignment (a class with private/public identifiers and three or more functions that use parameters) and avoid undefined behaviour, use a fixed-size container, keep loop counters local, initialize accumulators, validate input, and give several class methods that accept parameters (streams, counts). Avoid stdafx.h unless the project requires precompiled headers, and replace system("PAUSE") with a portable alternative.

Example class that meets the requirements (read/compute/print functions accept parameters, handles bad input, uses modern containers):

#include <iostream>
#include <array>
#include <limits>
#include <iomanip>

class RealStats {
    std::array<double, 10> vals{};
public:
    void read(std::istream& in = std::cin) {
        for (std::size_t i = 0; i < vals.size(); ++i) {
            while (true) {
                std::cout << "Enter number " << (i + 1) << ": ";
                if (in >> vals[i]) break;
                std::cout << "Invalid input, try again.\n";
                in.clear();
                in.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
            }
        }
    }

    double sum(std::size_t count) const {
        double s = 0;
        for (std::size_t i = 0; i < count && i < vals.size(); ++i) s += vals[i];
        return s;
    }

    double average(std::size_t count) const {
        return count ? sum(count) / static_cast<double>(count) : 0.0;
    }

    void print(std::ostream& out, std::size_t count) const {
        out << "Sum = " << sum(count) << '\n'
            << "Average = " << std::fixed << std::setprecision(6) << average(count) << '\n';
    }
};

Quick tips: compile with g++ -std=c++17 -Wall file.cpp (or keep stdafx.h if Visual Studio project uses precompiled headers). Test with non-numeric input to confirm the validation loop works. Replace the hard-coded 10 with a named constant if you need to change the count. This version satisfies the "three or more functions using parameters" requirement (read takes a stream, sum/average/print take parameters) and is safer and more portable than the original.

Recommended Answers

All 2 Replies

Member Avatar for Member #46692

Erm didn't you just ask this question before.?

[IMG][/IMG]
Piworld ™
[Tis simple as Pie]

#include "stdafx.h"
#include <iostream>
using namespace std;

// class with identifiers and functions
class real
	{
private:
	double n[10],s;						// declarations
	int i;
	double avg;
	
public:
	real()
	{
		s=0;
	}

	 void read()
	
	 {
		 cout<<"Please Enter 10 Numbers"<<endl;
		for (i=0;i<10;i++)
		{
			
			cout<<"You have Entered Number:";
		cin>>n[i];
		}
	 };

	void print()
	{
	
		for (i=0;i<10;i++)
		s=s+n[i];

	
		avg=s/10;
	
	
		cout<<"The Sum           ="<<s<<endl;
		cout<<"The Average	  ="<<avg<<endl;
	}
};
	int main()
{
    real a;

    a.read();
    
	a.print();
system("PAUSE");
    return 0;
}

ur code looks pretty alright this time..... it shud work.... or r u still having any problem?.... n wat kinda problem?.... one more thing to ask u.... did u put the class function in the stdafx.h file?.... or y did u include this file?.... take it easy! :)

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.