My c code isnt working I need to stream in an array output i

My c++ code isn\'t working! I need to stream in an array, output it, output it normalized(each value gets divided by the max), and prompt for a value and search the array for it, then output how many times it was found!


#include<iostream>
#include<fstream>

using namespace std;

int main()
{
//Declare Variables

   double search_value;
   double max;
   int search_tally, x(0),k(0);
   const int ARRAY_MAX = 20;
   double stats[ARRAY_MAX], New_Array[ARRAY_MAX],normal[ARRAY_MAX];

//Create file

   ifstream data_in;

//Open file

   data_in.open(\"P:\\\\Private\\\\CMPSC\\\\stats.txt\");

//Check file

   if (data_in.fail())
   {
       cout << \"Error Opening stats.txt\" << endl;
       system(\"pause\");
       return 0;

       }
//Prompt for Desired Value

   cout << \"What value do you want to look for?\" << endl;
   cin >> search_value;

cout << \"Original Stats Array:\ \ \" << \"Subscript\" << \"\\t\" << \"Value\" << endl;

//Loop to read in and use file

   do
   {
       data_in >> stats[x];

       k++;
      
       cout << k << \"\\t\\t\" << stats[x] << endl;

   } while (k<ARRAY_MAX);

   double Max_Value(double stats[], int count);
   double Normalization(double stats[], int count, double normal[]);
   double Search(double stats[], int count, int search);


//Call for Max_Value function
   max = Max_Value(stats, ARRAY_MAX);

//Call for normalizing function

   New_Array[ARRAY_MAX] = Normalization(stats, ARRAY_MAX, normal);

   cout << \"New Stats Array:\ \ \" << \"Subscript\" << \"\\t\" << \"Value\" << endl;
  
   for (x = 0;x < ARRAY_MAX;x++)
   {
       cout << x << \"\\t\\t\" << normal[k] << endl;
   }
  
//Call for search function
   search_tally=Search(stats,ARRAY_MAX,search_value);

//Output and echo
   cout << \"Your maximum value is\" << max << endl;
   cout << \"\ The value you entered is\" << search_value << endl;
   cout << \"\ Your value was listed\" << search_tally << \"times\" << endl;
   cout << \"\ Your normalized array is \" <<\"something \"<<endl; //HOW DO I DO THIS

   data_in.close();

       system(\"Pause\");
       return 0;
}

double Max_Value(double stats[], int count)

{

//Declare Variables
   int k;
   double max;
   max = stats[0];

//Loop to find max
   for ( k = 1;k <count;k++)
   {
       if (stats[k] > max)
       {
           max = stats[k];
       }
       return max;
   }

//Return max
  
  

}


/*

double Normalization(double stats[], int count, double normal[])
{
   int k;
   double max;
   max = Max_Value(stats, count);
   for (k = 0;k < count;k++)
   {
       normal[k] = stats[k] / max;

   }


   return normal[k];

}

/*

double Search(double stats[], int count, int search)
{
   int k;
   int value_count(0);

   for (k = 0;k < count;k++)
   {
       if (stats[k] = search)
           value_count++;
   }

   return value_count;

}

I\'m having so much trouble with this! My output is:

What value do you want to look for?
1
Original Stats Array:

Subscript Value
1 85.64
2 15.48
3 48.12
4 15.46
5 87.15
6 84.63
7 45.64
8 63.42
9 36.54
10 22.22
11 18.54
12 27.94
13 38.41
14 49.12
15 55.14
16 61.99
17 70.41
18 80.15
19 91.48
20 45.67
New Stats Array:

Subscript Value
0 -9.25596e+61
1 -9.25596e+61
2 -9.25596e+61
3 -9.25596e+61
4 -9.25596e+61
5 -9.25596e+61
6 -9.25596e+61
7 -9.25596e+61
8 -9.25596e+61
9 -9.25596e+61
10 -9.25596e+61
11 -9.25596e+61
12 -9.25596e+61
13 -9.25596e+61
14 -9.25596e+61
15 -9.25596e+61
16 -9.25596e+61
17 -9.25596e+61
18 -9.25596e+61
19 -9.25596e+61
Your maximum value is45.67

The value you entered is1

Your value was listed20times

Your normalized array is something
Press any key to continue . . .

Which mean

1-My max finding function is wrong even though I\'m sure it isn\'t.

2-I still can\'t output the normalized array properly

3-My search function isn\'t working

I\'m suspecting that I\'ve streamed in the values wrong but the output for the original array is correct so I doubt that...

Solution

In the max function you were returning the value inside the for loop.

Also, the mistakes were that you were using two indices as iterators and had written one in place of the other. Never do that. As long there is no conflict, use only one variable as an iterator.

#include<iostream>
#include<fstream>

using namespace std;

int main()
{
//Declare Variables

    double search_value;
    double max;
    int search_tally, x(0),k(0);
    const int ARRAY_MAX = 20;
    double stats[ARRAY_MAX], normal[ARRAY_MAX];
    double *New_Array;

//Create file

    ifstream data_in;

//Open file

    data_in.open(\"P:\\\\Private\\\\CMPSC\\\\stats.txt\");

//Check file

    if (data_in.fail())
    {
        cout << \"Error Opening stats.txt\" << endl;
        system(\"pause\");
        return 0;

    }
//Prompt for Desired Value

    cout << \"What value do you want to look for?\" << endl;
    cin >> search_value;

    cout << \"Original Stats Array:\ \ \" << \"Subscript\" << \"\\t\" << \"Value\" << endl;

//Loop to read in and use file

    do
    {
        data_in >> stats[k];

        k++;

        cout << k << \"\\t\\t\" << stats[x] << endl;

    } while (k<ARRAY_MAX);

    double Max_Value(double stats[], int count);
    double * Normalization(double stats[], int count, double normal[]);
    double Search(double stats[], int count, int search);


//Call for Max_Value function
    max = Max_Value(stats, ARRAY_MAX);

//Call for normalizing function

    New_Array = Normalization(stats, ARRAY_MAX, normal);

    cout << \"New Stats Array:\ \ \" << \"Subscript\" << \"\\t\" << \"Value\" << endl;

    for (k = 0; k < ARRAY_MAX; k++)
    {
        cout << k << \"\\t\\t\" << normal[k] << endl;
    }

//Call for search function
    search_tally=Search(stats,ARRAY_MAX,search_value);

//Output and echo
    cout << \"Your maximum value is\" << max << endl;
    cout << \"\ The value you entered is\" << search_value << endl;
    cout << \"\ Your value was listed\" << search_tally << \"times\" << endl;
    cout << \"\ Your normalized array is \" <<\"something \"<<endl; //HOW DO I DO THIS

    data_in.close();

        system(\"Pause\");
        return 0;
}

double Max_Value(double stats[], int count){
    //Declare Variables
    int k;
    double max;
    max = stats[0];

    //Loop to find max
    for (k = 1; k<count; k++)
    {
        if (stats[k] > max)
        {
            max = stats[k];
        }
        //return max;
    }
    return max;
    //Return max
}


double * Normalization(double stats[], int count, double normal[])
{
    int k;
    double max;
    max = Max_Value(stats, count);
    for (k = 0;k < count;k++)
    {
        normal[k] = stats[k] / max;
    }
    return normal;

}


double Search(double stats[], int count, int search)
{
    int k;
    int value_count(0);

    for (k = 0;k < count;k++)
    {
        if (stats[k] == search)
            value_count++;
    }

    return value_count;

}

My c++ code isn\'t working! I need to stream in an array, output it, output it normalized(each value gets divided by the max), and prompt for a value and search
My c++ code isn\'t working! I need to stream in an array, output it, output it normalized(each value gets divided by the max), and prompt for a value and search
My c++ code isn\'t working! I need to stream in an array, output it, output it normalized(each value gets divided by the max), and prompt for a value and search
My c++ code isn\'t working! I need to stream in an array, output it, output it normalized(each value gets divided by the max), and prompt for a value and search
My c++ code isn\'t working! I need to stream in an array, output it, output it normalized(each value gets divided by the max), and prompt for a value and search
My c++ code isn\'t working! I need to stream in an array, output it, output it normalized(each value gets divided by the max), and prompt for a value and search

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site