C++ :: Calculate Mean Of Array Class?
Jan 16, 2013
I am trying to create a small function that can return the mean of an object of array class, not a simple array.
Declare and initiate the array:
array<double, 100> myArray = {};
myArray.fill(1.0);
function to calculate mean:
double mean_array(double *array)
{
double sum = 0.0;
for(int i=0; i<sizeof(array); i++)
sum += array[i];
return sum / sizeof(array);
}
However, it does not work. It seems it is incompatible with the array definition.
View 8 Replies
ADVERTISEMENT
Feb 22, 2015
I have been trying to calculate the bond price given a certain YTM using class method. Since the YTM and bond price are linked with each other(there is another question asking us to calculate the YTM with a given price),we put price and YTM in "private" and others variables in"public". Actually, this is the format given in my assignment.
I used "getvalue" to get the value of YTM, and used YTM in the formula of bond price calculation. However, the output of the price is infinite, while the output of price is right if I used a specific value of YTM(such as 0.05) in the formula. It means that I didn't successfully get right value of YTM.
#include <iostream>
#include <cmath>
using namespace std;
class bond{
public:
bond(){};
[Code] .....
View 1 Replies
View Related
Sep 7, 2013
// Program to calculate fine using class.
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<string.h>
class library {
long bookid;
char bookname[25];
[Code] ....
In the above program, how do I call the diff. constructors in main?
View 5 Replies
View Related
Apr 1, 2014
Is this the correct way to calculate the offset of a 3d array? Also is the dynamic allocation for it in the main program correct?
Some how it's not working fine after I input the values for some values of x,y,z e.g when the const ints are 2.
#include <iostream>
using namespace std;
const int x = 2;
const int y = 2;
[Code]....
View 8 Replies
View Related
Mar 19, 2013
I'm having trouble figuring out how to calculate the sum of two lines in the array(for loop)? And the next part, I can return the values to the main program however, how do I do it with c pointers?
(1) Write a C function that takes an integer array argument. The array argument has two rows and NDATA columns where NDATA is a symbolic constant. For each column in the array argument, the function calculates the sum of the values in the first and second row. The function returns to its calling program (using a second argument and call by reference as implemented with C pointers) the maximum of these sums. Additionally the function returns the column subscript where this maximum first occurs. This subscript value is returned the usual way (using a return statement). etc...
Here is the start of the array program
Code:
#include<stdio.h>
#include<stdlib.h>
#define NDATA 5 /* define number of data per row */
int row; /* count rows */
int column; /* count columns */
[Code] ....
View 2 Replies
View Related
Jan 8, 2014
i wrote following code to calculate average of the values entered to the array.After displaying the output following error was displayed.
"Run-Time Check Failure #2 - Stack around the variable 'marks' was corrupted.
A buffer overrun has occurred in q 3 410005111.exe which has corrupted the program's internal state. Press Break to debug the program or Continue to terminate the program. "
//Average mark of student 1
cout<<"Avarage mark of the student 1 :"<<(marks[0][0]+marks[1][0]+marks[2][0]+marks[3][0]);
cout<<endl;
View 5 Replies
View Related
Feb 16, 2015
i'm having trouble trying to make this code works .. the program must repeatedly asks user for the age of a person until user enters -1 ,, once he/she entered -1 the program calculates ( smallest value entered, largest value , and average )excluding -1 from calculation
i've been working on this for a weeks this is the best i could do.i know average must add all the entered values , not only smallest and largest but how can i do that here ?
Code:
#include<stdio.h>
int main(void)
{
int age ;
int v;
int counter;
int l=0;
int s=0;
int limit;
}
[code]....
View 13 Replies
View Related
Mar 17, 2013
I'm writing a program that calculates the mode, mean and median of an array of integers. I've done the mean and median but i can't figure out mode.
I have to find the number of modes.
Ex array of integers: 10, 10, 4, 5, 6,5, 6, 7
My function has to return the number 3. Because there are 3 sets of mode; 10,5,6.
How do i implement this in my function because for now it just calculates the mode. It only return the number mode 10.
View 5 Replies
View Related
Oct 23, 2013
Use function decomposition.
-To prompt user to enter value and assign it to correct position in array
-calculate the average of an array
-display all element.
-display average.
For some reason, i keep getting error and it doesn't compile. I use Dev-C++
Here is my code:
#include <iostream>
#include <iosmanip>
#include <math.h>
using namespace std;
const int SIZE = 5;
const int LINE = 4;
//function prototypes
void getData(int arr[]);
[Code]...
My error is on line 69. I don't see anything wrong with it.
If i take out setw(3), it doesnt get any error but for some reason it doesnt run. Possible i did something wrong there?
View 2 Replies
View Related
Feb 21, 2014
I have written two separate programs; Program 1 calculates the average of an array of numbers which has been hard-coded into the program. Program 2 reads an array of numbers from a text file and displays them on the output.
I wish to combine these two programs into one by making Program 1 read the array of numbers from the file and then calculate the average using that instead of the array { 84, 92, 76, 81, 56 } as outlined below. I only wish to display the average in the output, not the number array as Program 2 does.
I have tried to do most of the work, I just need modifying the code slightly so it reads the number array from the file and calculates the average.
Program 1
#include <iostream>
#include <cmath>
#include <math.h>
#include <fstream>
#include <string>
#include <numeric>
using namespace std;
int main() {
const int nNumStudents = 5;
[Code] .....
View 7 Replies
View Related
Sep 12, 2013
I'm trying to calculate the number of distinct values entered into an array. If i enter the followings "3,4,5,6,7,7,6,e (anything that's not a number)" . I get a total of 7 but in reality it should be a 5.
Code:
#include <stdio.h>
//---------function to find the distinct values----
int find_distinct(int list[], int size)
{
int i, j,size2, distinct = 0;
for(i = 0; i < size; i++)
[Code]...
View 9 Replies
View Related
Nov 18, 2014
exercise in c: write a program which calculates % of even numbers entered by a user in an array;
1) check if user hasn't entered float value; if has issue warning;
2) give the option for user to press "stop" to exit program any time.
int c,sk[100],i,sum1=0, sum2=0, rel;
printf ("How many numbers will you enter?
");
scanf ("%d", &c);
printf ("Enter %d numbers (to exit program press - stop)
", c);
[Code] ....
View 1 Replies
View Related
Mar 21, 2015
In this book, item 3 is about never treat arrays polymorphically. In the latter part of this item, the author talks about the result of deleting an array of derived class objects through a base class pointer is undefined. What does it mean? I have an example here,
Code:
class B
{
public:
B():_y(1){}
virtual ~B() {
cout<<"~B()"<<endl;
[Code] ....
This sample code does exactly what I want. So does the author mean the way I did is undefined?
View 1 Replies
View Related
Jan 31, 2014
I have to create a program that calculates the final scores per diver, who each get five dives. I have to include the difficulty level when figuring out this score, and I need to drop the highest and lowest scores.
Now, I'm reading from a file where the values are something like:
234 :Diver number
2.3 :Difficulty
2.1 3.2 4.0 2.3 3.8 :Scores
Would I create a parallel array for the difficulty, or include it in a 2D array with the scores. Something like
score[Difficulty][individualScores]; ?
Also, would I include a findMin and findMax in a function that calculates the total, seeing as how the highest and lowest must be dropped to determine the final score?
View 11 Replies
View Related
Jan 24, 2014
I have this code that im stuck on what i need to do is Extend the code such that when it starts the user is asked to input values to specify each of the three ranges before moving on to accept and tally the main values how do i do that Using a for loop to input and output array values Also calculate the average
*/
#include <stdio.h>
int main(void)
{
/* Declare an array of integers */
int Grades[5];
int nCount;
int nTotal = 0; /* Declare and initialise the value */
float fAverage;
[Code]...
View 1 Replies
View Related
Apr 11, 2013
it compiles and runs, it gives me the average and the highest score but says lowest is 0 even though 0 is never entered.
Write a program which uses an array of 5 scores and calculates the average score, the highest score and the lowest score.
#include <iostream>
using namespace std;
int getHighest(int [], int);
int getSmallest(int [], int);
double getAverage(int [], int);
int count;
int numbers;
[Code] ......
View 2 Replies
View Related
Jan 16, 2013
Please consider the following code :
#include <iostream>
using namespace std;
class superclass;
class subclass1;
class subclass2;
[Code] ....
As you can see I want to create a dynamically allocated storage of references to a parent class each of which can then point to a child class, how ever I do not know how to extract the child class out again from that array so i may access its variable b.
View 2 Replies
View Related
Apr 8, 2014
Base class has an array, whose size is controlled by the derived class.
I can't use the STL and use a 2003 compiler, so things like std::vector and std::array are out. I also can't use dynamic memory allocation.
So I thought of a few options:
1.
template <int N> class myBaseClass { ... int array[N]; ... }
then class MyClass: public myBaseClass<8> ... etc ...
2.
have a int **array in the base and assign in the derived class.
3.
give the base some virtual methods such as int *getArray or even int &getInt for more safety.
View 6 Replies
View Related
Mar 26, 2014
I'm trying to make a c++ program of this but i don't know how to use the bubble sorting.
Write a C++ application that asks the user to enter 10 numbers. The program then stores those numbers in an Array. The program should display the Mean , Median, and Mode.
Mean is the average of the 10 numbers.
Median is the average of the 5th and the 6th numbers. (But you have to arrange the numbers in ascending order first using Bubble Sort before calculating the Median.)
Mode is the most frequent number among the 10 numbers.
View 5 Replies
View Related
May 2, 2013
How do I go about storing info to an array of class?I need to do something like this:
info[count] = ReadNameRating(n, r);
Where info is an array of my own class and ReadNameRating reads the name and rating which is two of the variables handled by that class.
View 2 Replies
View Related
Oct 12, 2014
class FlashCard {
public:
int a,b;
void PrintCard(void);
int CorrectAnswer(void);
};
void SwapFlashCard(FlashCard &a,FlashCard &b)
[Code]....
Why does Xode warn me that Type'FlashCard'does not provide a subscript operator on line 22,23,29 and 30?
View 7 Replies
View Related
Mar 24, 2014
Version 1 (works fine)
Creating array in main and then calling a sorting function from sorting class.
In main:
const size_t SIZE = 100;
int *array = new int [SIZE];
//fill array with ints, not shown here
quickSort(array, SIZE); //calling the sorting function in the sorting class
In the sorting class, quickSort is declared as such:
void quickSort(int arr[], int num);
Everything works great.
Version 2 (issues)
Instead of creating the array in main, I have set up a class for the array, MyArrayClass, where I create an object containing the array of ints. So far so good. The issues come when I write a member function to call quickSort. Even though my MyArrayClass object contains an array of ints, the code calling for quickSort() won't compile as the data type isn't ints but MyArrayClass (which in turn holds ints though). The compiler (using VS 2013 btw) complains that quickSort can’t convert the first argument from 'const MyArrayClass' to 'int[]'.
How do I cast my class object array of ints as an int[] in order to be able to call the quickSort function? Or should I solve this issue in some other way? I tried altering the sorting function to accept the object as it is, but that only created an avalanche of new errors, so thinking that converting/casting the object array --> int[] might be easier...
View 6 Replies
View Related
May 13, 2014
I need an array of class objects but am unsure of how one might accomplish this. I have so far...
//element class driver code
Element Arsenic(lowCeiling, highCeiling);
Element Cadmium(lowCeiling, highCeiling);
Element Chromium(lowCeiling, highCeiling);
Element Copper(lowCeiling, highCeiling);
Element Lead(lowCeiling, highCeiling);
Element Nickel(lowCeiling, highCeiling);
Element Molybdenum(lowCeiling, highCeiling);
Element Mercury(lowCeiling, highCeiling);
Element Selenium(lowCeiling, highCeiling);
Element Zinc(lowCeiling, highCeiling);
Element metal[10] = {Arsenic, Cadmium, Chromium, Copper, Lead, Nickel, Molybdenum, Mercury, Selenium, Zinc};
Could the array be created in this manner or is that illegal code?
View 2 Replies
View Related
Mar 20, 2014
I want to make Class for 2D array.
This is part of Class Definition.
class CMatrix{
public:
explicit CMatrix( int rows, int cols ){
m_pArray = new float[ rows * cols ];
m_nRows = rows;
[Code].....
View 6 Replies
View Related
Jan 10, 2015
I have an inventory array in a class called inventory. This class is in a different program. How do I access and add to this array for my main program?
View 1 Replies
View Related
Jun 6, 2012
Shape base class, line and Point derived classes. What should I declare in .h files and implement in .cpp files that this is array will be work.
My major concern refer to operator [] and assign (=) operator. As far as I understand I should overload ([]) and (=) three times for classes shape , line and point or not... or is it possible made through virtual function? How will be code looks like ?
Code:
// part of main.cpp
Shape* shapes[3]; // Array of pointers to Shape
shapes[0] = new Shape();
shapes[1] = new Line ("line from array ", Point(1,22),Point(33,22));
shapes[2] = new Point(11,44);
cout << "using ToString function" << endl;
for(int i=0; i < 3; i++)
cout << s[i]->ToString();
for(i=0; i < 3; i++)
delete s[i];
View 2 Replies
View Related