Here is my code for a project for class. It tells me that \'data\' is ambiguous.
ID: 3739651 • Letter: H
Question
Here is my code for a project for class. It tells me that 'data' is ambiguous. Please fix this error.
#include<iostream>
#include<fstream>
#include <cstdlib>
#include<string>
#include<stdlib.h>
#include<cmath>
#include <iomanip> // Allows for setprecision to happen
using namespace std;
// Declaring Variables
float data[200];
int n; // number of elements
float std_dev, average;
void std_deviation(float var);
float variance() // finding the variance
{
int i;
float sum = 0, sum1 = 0, var;
for (i = 0; i < n; i++){
sum = sum + data[i];
}
average = sum / (float)n; // finding the mean
/* Compute variance and standard deviation */
for (i = 0; i < n; i++){
sum1 = sum1 + pow((data[i] - average), 2);
}
var = sum1 / (float)n;
std_deviation(var);
return var; // The variance for the set of Data.
}
void std_deviation(float var) // finding standard deviation
{
std_dev = sqrt(var);
}
float Median() { // finding the median
float * sorted = new float[n];
for (int i = 0; i < n; ++i) {
sorted[i] = data[i];
}
for (int i = n - 1; i > 0; --i) {
for (int j = 0; j < i; ++j) {
if (sorted[j] > sorted[j + 1]) { // Sorting the numbers in order to find the median.
float dTemp = sorted[j];
sorted[j] = sorted[j + 1];
sorted[j + 1] = dTemp;
}
}
}
float median = 0.0;
if ((n % 2) == 0) {
median = (sorted[n / 2] + sorted[(n / 2) - 1]) / 2.0;
} else {
median = sorted[n / 2];
}
delete [] sorted;
return median; // The actual median for a set of data.
}
float Mode() { //finding the mode
int * repeat = new int[n];
for (int i = 0; i < n; ++i) {
repeat[i] = 0;
int j = 0;
bool bFound = false;
while ((j < i) && (data[i] != data[j])) {
if (data[i] != data[j]) {
++j;
}
}
++(repeat[j]);
}
int MaxRepeat = 0;
for (int i = 1; i < n; ++i) {
if (repeat[i] > repeat[MaxRepeat]) {
MaxRepeat = i;
}
}
delete [] repeat;
return data[MaxRepeat];
}
float Range() { // finding the range
float highest = data[0], range;
float lowest = data[0];
for(int i = 1; i < n; i++)
{
if (highest < data[i]) {
highest = data[i];
}
if (lowest > data[i]) {
lowest = data[i];
}
}
range = highest - lowest;
return range; // The actual range for the set of Data.
}
int main()
{
char word[10];
int i = 0;
ifstream
infile("ArsenicGroundWaterFile.rtf");
while(infile >> word)
{
data[i++] = atof(word);
}
n = i;
cout << "Variance : " << fixed << setprecision(2) << variance() << " ";
cout << "Mean : " << fixed << setprecision(2) << average << " ";
cout << "Mode : " << fixed << setprecision(2) << Mode() << " ";
cout << "Median : " << fixed << setprecision(2) << Median() << " ";
cout << "std_deviation : " << fixed << setprecision(2) << std_dev << " ";
cout << "Range : " << fixed << setprecision(2) << Range() << " ";
}
Here are all the numbers that are being tested. They must be in a file (.rtf).
17.6
10.4
13.5
4
19.9
16
12
12.2
11.4
12.7
3
10.3
21.4
19.4
9
6.5
10.1
8.7
9.7
6.4
9.7
63
15.5
10.7
18.2
7.5
6.1
6.7
6.9
0.8
73.5
12
28
12.6
9.4
6.2
15.3
7.3
10.7
15.9
5.8
1
8.6
1.3
13.7
2.8
2.4
1.4
2.9
13.1
15.3
9.2
11.7
4.5
1
1.2
0.8
1
2.4
4.4
2.2
2.9
3.6
2.5
1.8
5.9
2.8
1.7
4.6
5.4
3
3.1
1.3
2.6
1.4
2.3
1
5.4
1.8
2.6
3.4
1.4
10.7
18.2
7.7
6.5
12.2
10.1
6.4
10.7
6.1
0.8
12
28.1
9.4
6.2
7.3
9.7
62.1
15.5
6.4
9.5
Explanation / Answer
The followwing code is working perffectly fine, you just need to check your .rtf file. It should have name ArsenicGroundWaterFile.rtf . And should be in same folder as the compiled file is running.
Output of the code:
dps@machine:~/Desktop$ g++ fix.cc -o fix
#include<iostream>
#include<fstream>
#include <cstdlib>
#include<string>
#include<stdlib.h>
#include<cmath>
#include <iomanip> // Allows for setprecision to happen
using namespace std;
// Declaring Variables
float data[200];
int n; // number of elements
float std_dev, average;
void std_deviation(float var);
float variance() // finding the variance
{
int i;
float sum = 0, sum1 = 0, var;
for (i = 0; i < n; i++){
sum = sum + data[i];
}
average = sum / (float)n; // finding the mean
/* Compute variance and standard deviation */
for (i = 0; i < n; i++){
sum1 = sum1 + pow((data[i] - average), 2);
}
var = sum1 / (float)n;
std_deviation(var);
return var; // The variance for the set of Data.
}
void std_deviation(float var) // finding standard deviation
{
std_dev = sqrt(var);
}
float Median() { // finding the median
float * sorted = new float[n];
for (int i = 0; i < n; ++i) {
sorted[i] = data[i];
}
for (int i = n - 1; i > 0; --i) {
for (int j = 0; j < i; ++j) {
if (sorted[j] > sorted[j + 1]) { // Sorting the numbers in order to find the median.
float dTemp = sorted[j];
sorted[j] = sorted[j + 1];
sorted[j + 1] = dTemp;
}
}
}
float median = 0.0;
if ((n % 2) == 0) {
median = (sorted[n / 2] + sorted[(n / 2) - 1]) / 2.0;
} else {
median = sorted[n / 2];
}
delete [] sorted;
return median; // The actual median for a set of data.
}
float Mode() { //finding the mode
int * repeat = new int[n];
for (int i = 0; i < n; ++i) {
repeat[i] = 0;
int j = 0;
bool bFound = false;
while ((j < i) && (data[i] != data[j])) {
if (data[i] != data[j]) {
++j;
}
}
++(repeat[j]);
}
int MaxRepeat = 0;
for (int i = 1; i < n; ++i) {
if (repeat[i] > repeat[MaxRepeat]) {
MaxRepeat = i;
}
}
delete [] repeat;
return data[MaxRepeat];
}
float Range() { // finding the range
float highest = data[0], range;
float lowest = data[0];
for(int i = 1; i < n; i++)
{
if (highest < data[i]) {
highest = data[i];
}
if (lowest > data[i]) {
lowest = data[i];
}
}
range = highest - lowest;
return range; // The actual range for the set of Data.
}
int main()
{
char word[10];
int i = 0;
ifstream
infile("ArsenicGroundWaterFile.rtf");
while(infile >> word)
{
data[i++] = atof(word);
}
n = i;
cout << "Variance : " << fixed << setprecision(2) << variance() << " ";
cout << "Mean : " << fixed << setprecision(2) << average << " ";
cout << "Mode : " << fixed << setprecision(2) << Mode() << " ";
cout << "Median : " << fixed << setprecision(2) << Median() << " ";
cout << "std_deviation : " << fixed << setprecision(2) << std_dev << " ";
cout << "Range : " << fixed << setprecision(2) << Range() << " ";
}
ArsenicGroundWaterFile.rtf file data
17.6
10.4
13.5
4
19.9
16
12
12.2
11.4
12.7
3
10.3
21.4
19.4
9
6.5
10.1
8.7
9.7
6.4
9.7
63
15.5
10.7
18.2
7.5
6.1
6.7
6.9
0.8
73.5
12
28
12.6
9.4
6.2
15.3
7.3
10.7
15.9
5.8
1
8.6
1.3
13.7
2.8
2.4
1.4
2.9
13.1
15.3
9.2
11.7
4.5
1
1.2
0.8
1
2.4
4.4
2.2
2.9
3.6
2.5
1.8
5.9
2.8
1.7
4.6
5.4
3
3.1
1.3
2.6
1.4
2.3
1
5.4
1.8
2.6
3.4
1.4
10.7
18.2
7.7
6.5
12.2
10.1
6.4
10.7
6.1
0.8
12
28.1
9.4
6.2
7.3
9.7
62.1
15.5
6.4
9.5
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.