Fellow C++ citizens,

I am very new to C++ and am trying to do the following:

I want to transform a chart into code. Basically, the chart serves as a diagnostic reference. For an example, if a certain condition is below a certain value, there would be a few issues associated with this condition. The diagnosis will run through a series of conditions, and the number of the time that the problems (only 9 possible) appear is tallied up. Here is the code so far:

#include <cstdlib>
#include <iostream>

using namespace std;

int main ()
{
    // Declaring Variables
    int ambient_temp;
    int condenser_temp;
    int normal_cond_temp;
    int cond_outlet_temp;
    int condenser_subcool;
    int evap_temp;
    int normal_evap_temp;
    int air_entr_evap;
    int evap_superheat;
    int normal_superheat;
    int evap_outlet_temp;
    int sightglass;
    int problem[9];
    
    // Determining Condensing Temperature
    cout << "Please enter Ambient Temp: ";
    cin >> ambient_temp;
    cout << "Please enter Condensing Temperature: ";
    cin >> condenser_temp;
    normal_cond_temp = ambient_temp + 30;
    
    if (condenser_temp > normal_cond_temp + 10)
    {
       problem[1] = 0;
       problem[2] = 0;
       problem[3] = 0;
       problem[4] = 0;
       problem[5] = 0;
       problem[6] = 1;
       problem[7] = 1;
       problem[8] = 1;
       problem[9] = 1;
    }
    else if (condenser_temp < normal_cond_temp - 10)
    {
       problem[1] = 1;
       problem[2] = 1;
       problem[3] = 1;
       problem[4] = 1;
       problem[5] = 1;
       problem[6] = 0;
       problem[7] = 0;
       problem[8] = 0;
       problem[9] = 0;
    }
    else
    {
        problem[1] = 0;
        problem[2] = 0;
        problem[3] = 0;
        problem[4] = 0;
        problem[5] = 0;
        problem[6] = 0;
        problem[7] = 0;
        problem[8] = 0;
        problem[9] = 0;
    }

    // Determining Condenser Subcooling
    cout << "Please enter Condensing Outlet Temp: ";
    cin >> cond_outlet_temp;
    condenser_subcool = condenser_temp - cond_outlet_temp;

    if (condenser_subcool > 20)
    {
       problem[1] += 0;
       problem[2] += 1;
       problem[3] += 0;
       problem[4] += 0;
       problem[5] += 0;
       problem[6] += 1;
       problem[7] += 0;
       problem[8] += 1;
       problem[9] += 1;
    }
    else if (condenser_subcool < 5)
    {
       problem[1] += 0;
       problem[2] += 0;
       problem[3] += 1;
       problem[4] += 0;
       problem[5] += 0;
       problem[6] += 0;
       problem[7] += 0;
       problem[8] += 0;
       problem[9] += 0;
    }
    else
    {
        problem[1] += 1;
        problem[2] += 1;
        problem[3] += 0;
        problem[4] += 1;
        problem[5] += 1;
        problem[6] += 0;
        problem[7] += 1;
        problem[8] += 0;
        problem[9] += 0;
    }

    // Determining Evaporator Temperature
    cout << "Please enter Air Entry Evap Temp: ";
    cin >> air_entr_evap;
    cout << "Please enter Evaporator Temp: ";
    cin >> evap_temp;
    normal_evap_temp = air_entr_evap - 35;
    
    if (evap_temp > normal_evap_temp + 10)
    {
       problem[1] += 0;
       problem[2] += 0;
       problem[3] += 0;
       problem[4] += 1;
       problem[5] += 0;
       problem[6] += 0;
       problem[7] += 1;
       problem[8] += 1;
       problem[9] += 1;
    }
    else if (evap_temp < normal_evap_temp - 10)
    {
       problem[1] += 1;
       problem[2] += 1;
       problem[3] += 1;
       problem[4] += 0;
       problem[5] += 1;
       problem[6] += 1;
       problem[7] += 0;
       problem[8] += 0;
       problem[9] += 0;
    }
    else
    {
        problem[1] += 0;
        problem[2] += 0;
        problem[3] += 0;
        problem[4] += 0;
        problem[5] += 0;
        problem[6] += 0;
        problem[7] += 1;
        problem[8] += 1;
        problem[9] += 1;
    }

    // Determining Evaporator Superheat
    cout << "Please enter Evaporator Outlet Temp: ";
    cin >> evap_outlet_temp;
    evap_superheat = evap_temp - evap_outlet_temp;
    normal_superheat = 15;
    
    if (evap_superheat > normal_superheat + 5)
    {
       problem[1] += 0;
       problem[2] += 1;
       problem[3] += 1;
       problem[4] += 1;
       problem[5] += 1;
       problem[6] += 1;
       problem[7] += 0;
       problem[8] += 0;
       problem[9] += 0;
    }
    else if (evap_superheat < normal_superheat - 5)
    {
       problem[1] += 1;
       problem[2] += 0;
       problem[3] += 0;
       problem[4] += 0;
       problem[5] += 0;
       problem[6] += 0;
       problem[7] += 1;
       problem[8] += 1;
       problem[9] += 1;
    }
    else
    {
        problem[1] += 1;
        problem[2] += 0;
        problem[3] += 0;
        problem[4] += 0;
        problem[5] += 0;
        problem[6] += 0;
        problem[7] += 1;
        problem[8] += 1;
        problem[9] += 1;
    }
    
    // Determining Sight Glass Issues
    cout << "Is Sight Glass 1.Full or 2.Bubbling: ";
    cin >> sightglass;
    
    if (sightglass == 1)
    {
        problem[1] += 1;
        problem[2] += 1;
        problem[3] += 0;
        problem[4] += 1;
        problem[5] += 1;
        problem[6] += 0;
        problem[7] += 1;
        problem[8] += 1;
        problem[9] += 1;
    }
    else
    {
        problem[1] += 0;
        problem[2] += 0;
        problem[3] += 1;
        problem[4] += 0;
        problem[5] += 1;
        problem[6] += 1;
        problem[7] += 0;
        problem[8] += 0;
        problem[9] += 0;
    }
    
    
    cout << endl;
    // System Pause
    system("PAUSE");
    return EXIT_SUCCESS;

}

However, now I run into the issue of extracting useful information from this system. I want to know which problem came up most frequently. To do this, I need to be able to identify the maximum value of the element in the array and its associated index. In addition, I also want to be able to label the problems with strings as opposed to the array index number. What can I do?

Thank you so much for your time!

This is what I ended up writing.

#include <cstdlib>
#include <iostream>

using namespace std;

void compare(int problem[], int SIZE);

int main(){

.
.
.
.

/* I placed this following section at the very end of your program, between the last else statement and your system pause. */

    int SIZE = 10;   // you set up your array wrong, array start at 0, not 1.      
		// problem[0] is the first location in memory, not problem[1].         
                    // Suppose it was clarity from science to programming.

    compare(problem, SIZE);
    
    // System Pause
   system("PAUSE");
   return EXIT_SUCCESS;

}

void compare(int problem[], int SIZE) {
	int max = problem[1];
	for(int index = 1; index < SIZE; index++){
		if(max < problem[index])
			max = problem[index];
	}
	cout << "Maximum value from 1 - 9 in the array: " << max << endl;

	if(max == 1)
		cout << "Type string name 1 here" << endl;
	else if (max == 2)
		cout << "Type string name 2 here" << endl;
	else if (max == 3)
		cout << "Type string name 3 here" << endl;
	else if (max == 4)
		cout << "Type string name 4 here" << endl;
	else if (max == 5)
		cout << "Type string name 5 here" << endl;
	else if (max == 6)
		cout << "Type string name 6 here" << endl;
	else if (max == 7)
		cout << "Type string name 7 here" << endl;
	else if (max == 8)
		cout << "Type string name 8 here" << endl;
	else if (max == 9)
		cout << "Type string name 9 here" << endl;
}

I was lazy, but you can make this if/else section into a function itself. Darn my laziness! The only error I ended up getting was a debugging error,

Run-Time Check Failure #2 - Stack around the variable 'problem' was corrupted.

Which, after debugging it step-by-step was at the { at

return EXIT_SUCCESS;

}          /* debugger states problem here! For Check Failure 2. Very odd. 
           // but I do not get the error if you comment out my function. 
           // All-in-all, you want the for loop example that I used to check
           // through an array. I hope this helps!

AH. I got it!

int problem[9];

/* To get 9 object into the array, you need to declare as you did

int problem[9];

but you failed to realize, or at least looked over the small detail that the array starts at [0], and not [1], the array goes from problem[0] to problem[8] NOT problem[1] to problem[9]. 

If you change problem to problem[10] as such,

int problem[10];

Then you can skip the first entry [0], and continue as you did before. As such, the debugging error that I had received before is now gone. Copy and past what I have written, just change your array up top to,

int problem[10];

Saith is correct in pointing that you should declare array as

int problem[10];

I want to know which problem came up most frequently.

You can perform sorting on the array problem while using a separate array for index numbers.

In addition, I also want to be able to label the problems with strings as opposed to the array index number.

for this maintain a character 2D array with same index numbers as problem array.

//Example
int problem[10];
char problem_string[][]={
"";  //Empty String due to your convention of numbering
"Problem Number 1";
"Problem Number 2";
.
.
.//so on
};

printing and editing strings this way would be quite simple. But you can go with a switch case statement as well.

Vinayak

To do this, I need to be able to identify the maximum value of the element in the array and its associated index. In addition, I also want to be able to label the problems with strings as opposed to the array index number. What can I do?

Obviously you need to correct the error indicated by Saith

but you failed to realize, or at least looked over the small detail that the array starts at [0], and not [1], the array goes from problem[0] to problem[8] NOT problem[1] to problem[9].

To associate a string label with each problem, find the element with the maximum value, and print the associated label, create an array containing the labels:

void print_label( const int problem[], const char* const label[], int N )
{
   int max_value = problem[0] ;
   int max_index = 0 ;

   for( int i=1 ; i<N ; ++i ) if( max_value < problem[i] )
   {
       max_value = problem[i] ;
       max_index = i ;
   }

   std::cout << label[ max_index ] << '\n' ;
}

int main ()
{
    // ...
    enum { NPROBS = 9 } ; // number of problems
    int problem[ NPROBS ] = {0} ; // initialize to all zeroes
    const char* const  label[ NPROBS ] = { "label one", "label two", /* etc. */ } ;
    
    // ...

    print_label( problem, label, NPROBS ) ;
    // ...
}

Consider making your code data-driven. For example,

void assign( int dest[], const int srce[], int N )
{ for( int i=0 ; i<N ; ++i ) dest[i] = srce[i] ; }

void increment( int a[], const int incr[], int N )
{ for( int i=0 ; i<N ; ++i ) a[i] += incr[i] ; }

int main ()
{
    // ...

    enum { NPROBS = 9 } ; // number of problems
    int problem[ NPROBS ] = {0} ; // initialize to all zeroes
    const char* const  label[ NPROBS ] = { "label one", "label two", /* etc. */ } ;

    // Determining Condensing Temperature
    cout << "Please enter Ambient Temp: ";
    cin >> ambient_temp;
    cout << "Please enter Condensing Temperature: ";
    cin >> condenser_temp;
    normal_cond_temp = ambient_temp + 30;

    const int condenser_temp_high_values[ NPROBS ] = { 0, 0, 0, 0, 0, 1, 1, 1, 1 } ;
    const int condenser_temp_low_values[ NPROBS ] = { 1, 1, 1, 1, 1, 0, 0, 0, 0 } ;

    if (condenser_temp > normal_cond_temp + 10)
        assign( problem, condenser_temp_high_values, NPROBS ) ;
    else if (condenser_temp < normal_cond_temp - 10)
        assign( problem, condenser_temp_low_values, NPROBS ) ;
    // else nothing to be done; problem[] already contains zeroes


    // Determining Condenser Subcooling
    cout << "Please enter Condensing Outlet Temp: ";
    cin >> cond_outlet_temp;
    condenser_subcool = condenser_temp - cond_outlet_temp;

    const int condenser_subcool_high_increments[NPROBS] = { 0, 1, 0, 0, 0, 1, 1, 1, 1 } ;
    const int condenser_subcool_low_increments[NPROBS] = { 0, 0, 1, 0, 0, 0, 0, 0, 0 } ;
    const int condenser_subcool_normal_increments[NPROBS] = { 1, 1, 0, 1, 1, 0, 1, 0, 0 } ;

    if (condenser_subcool > 20)
         increment( problem, condenser_subcool_high_increments, NPROBS ) ;
    else if (condenser_subcool < 5)
         increment( problem, condenser_subcool_low_increments, NPROBS ) ;
    else
         increment( problem, condenser_subcool_normal_increments, NPROBS ) ;

    // Determining Evaporator Temperature
    // etc.
    // ...
}

Dear Saith, vinayakgarg, and vijayan121,

Thank you so much for imparting this knowledge to me and for all your help! I took your advice and rewrote the code - now the array is restructured and a sorting system is put in place. Additionally, the index number is able to be translated into regular text through a simple if-tree. The code is as follows:

#include <cstdlib>
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>


using namespace std;

// Sorting Mechanism while Preserving Index
typedef struct _amplitude_index
        { // Struct to store and order the values of the amplitudes preserving the index in the original array
	      int amplitude;
          int index;
        }
        t_amplitude_index;

        int compare_structs (const void *a, const void *b);

int main (int argc, const char * argv[])
{
    // Declaring Variables
    int ambient_temp;
    int condenser_temp;
    int normal_cond_temp;
    int cond_outlet_temp;
    int condenser_subcool;
    int evap_temp;
    int normal_evap_temp;
    int air_entr_evap;
    int evap_superheat;
    int normal_superheat;
    int evap_outlet_temp;
    int sightglass;
    
    // Creating Array for Problems
   	int length_array_range = 9;
	t_amplitude_index *problem;
	problem = (t_amplitude_index *) malloc(sizeof(t_amplitude_index) * length_array_range);
	
    int i;
	for(i = 0; i< length_array_range;i++)
    {
		problem[i].amplitude = 0;
		problem[i].index = i;
	}

    
    // Determining Condensing Temperature
    cout << "Please enter Ambient Temp: ";
    cin >> ambient_temp;
    cout << "Please enter Condensing Temperature: ";
    cin >> condenser_temp;
    normal_cond_temp = ambient_temp + 30;
    
    if (condenser_temp > normal_cond_temp + 10)
    {
       problem[0].amplitude = 0;
       problem[1].amplitude = 0;
       problem[2].amplitude = 0;
       problem[3].amplitude = 0;
       problem[4].amplitude = 0;
       problem[5].amplitude = 1;
       problem[6].amplitude = 1;
       problem[7].amplitude = 1;
       problem[8].amplitude = 1;
    }
    else if (condenser_temp < normal_cond_temp - 10)
    {
       problem[0].amplitude = 1;
       problem[1].amplitude = 1;
       problem[2].amplitude = 1;
       problem[3].amplitude = 1;
       problem[4].amplitude = 1;
       problem[5].amplitude = 0;
       problem[6].amplitude = 0;
       problem[7].amplitude = 0;
       problem[8].amplitude = 0;
    }
    else
    {
        problem[0].amplitude = 0;
        problem[1].amplitude = 0;
        problem[2].amplitude = 0;
        problem[3].amplitude = 0;
        problem[4].amplitude = 0;
        problem[5].amplitude = 0;
        problem[6].amplitude = 0;
        problem[7].amplitude = 0;
        problem[8].amplitude = 0;
    }

    // Determining Condenser Subcooling
    cout << "Please enter Condensing Outlet Temp: ";
    cin >> cond_outlet_temp;
    condenser_subcool = condenser_temp - cond_outlet_temp;

    if (condenser_subcool > 20)
    {
       problem[0].amplitude += 0;
       problem[1].amplitude += 1;
       problem[2].amplitude += 0;
       problem[3].amplitude += 0;
       problem[4].amplitude += 0;
       problem[5].amplitude += 1;
       problem[6].amplitude += 0;
       problem[7].amplitude += 1;
       problem[8].amplitude += 1;
    }
    else if (condenser_subcool < 5)
    {
       problem[0].amplitude += 0;
       problem[1].amplitude += 0;
       problem[2].amplitude += 1;
       problem[3].amplitude += 0;
       problem[4].amplitude += 0;
       problem[5].amplitude += 0;
       problem[6].amplitude += 0;
       problem[7].amplitude += 0;
       problem[8].amplitude += 0;
    }
    else
    {
        problem[0].amplitude += 1;
        problem[1].amplitude += 1;
        problem[2].amplitude += 0;
        problem[3].amplitude += 1;
        problem[4].amplitude += 1;
        problem[5].amplitude += 0;
        problem[6].amplitude += 1;
        problem[7].amplitude += 0;
        problem[8].amplitude += 0;
    }

    // Determining Evaporator Temperature
    cout << "Please enter Air Entry Evap Temp: ";
    cin >> air_entr_evap;
    cout << "Please enter Evaporator Temp: ";
    cin >> evap_temp;
    normal_evap_temp = air_entr_evap - 35;
    
    if (evap_temp > normal_evap_temp + 10)
    {
       problem[0].amplitude += 0;
       problem[1].amplitude += 0;
       problem[2].amplitude += 0;
       problem[3].amplitude += 1;
       problem[4].amplitude += 0;
       problem[5].amplitude += 0;
       problem[6].amplitude += 1;
       problem[7].amplitude += 1;
       problem[8].amplitude += 1;
    }
    else if (evap_temp < normal_evap_temp - 10)
    {
       problem[0].amplitude += 1;
       problem[1].amplitude += 1;
       problem[2].amplitude += 1;
       problem[3].amplitude += 0;
       problem[4].amplitude += 1;
       problem[5].amplitude += 1;
       problem[6].amplitude += 0;
       problem[7].amplitude += 0;
       problem[8].amplitude += 0;
    }
    else
    {
        problem[0].amplitude += 0;
        problem[1].amplitude += 0;
        problem[2].amplitude += 0;
        problem[3].amplitude += 0;
        problem[4].amplitude += 0;
        problem[5].amplitude += 0;
        problem[6].amplitude += 1;
        problem[7].amplitude += 1;
        problem[8].amplitude += 1;
    }

    // Determining Evaporator Superheat
    cout << "Please enter Evaporator Outlet Temp: ";
    cin >> evap_outlet_temp;
    evap_superheat = evap_temp - evap_outlet_temp;
    normal_superheat = 15;
    
    if (evap_superheat > normal_superheat + 5)
    {
       problem[0].amplitude += 0;
       problem[1].amplitude += 1;
       problem[2].amplitude += 1;
       problem[3].amplitude += 1;
       problem[4].amplitude += 1;
       problem[5].amplitude += 1;
       problem[6].amplitude += 0;
       problem[7].amplitude += 0;
       problem[8].amplitude += 0;
    }
    else if (evap_superheat < normal_superheat - 5)
    {
       problem[0].amplitude += 1;
       problem[1].amplitude += 0;
       problem[2].amplitude += 0;
       problem[3].amplitude += 0;
       problem[4].amplitude += 0;
       problem[5].amplitude += 0;
       problem[6].amplitude += 1;
       problem[7].amplitude += 1;
       problem[8].amplitude += 1;
    }
    else
    {
        problem[0].amplitude += 1;
        problem[1].amplitude += 0;
        problem[2].amplitude += 0;
        problem[3].amplitude += 0;
        problem[4].amplitude += 0;
        problem[5].amplitude += 0;
        problem[6].amplitude += 1;
        problem[7].amplitude += 1;
        problem[8].amplitude += 1;
    }
    
    // Determining Sight Glass Issues
    cout << "Is Sight Glass 1.Full or 2.Bubbling: ";
    cin >> sightglass;
    
    if (sightglass == 1)
    {
        problem[0].amplitude += 1;
        problem[1].amplitude += 1;
        problem[2].amplitude += 0;
        problem[3].amplitude += 1;
        problem[4].amplitude += 1;
        problem[5].amplitude += 0;
        problem[6].amplitude += 1;
        problem[7].amplitude += 1;
        problem[8].amplitude += 1;
    }
    else
    {
        problem[0].amplitude += 0;
        problem[1].amplitude += 0;
        problem[2].amplitude += 1;
        problem[3].amplitude += 0;
        problem[4].amplitude += 1;
        problem[5].amplitude += 1;
        problem[6].amplitude += 0;
        problem[7].amplitude += 0;
        problem[8].amplitude += 0;
    }
    
    // Code for sorting with qsort
	qsort(problem, length_array_range, sizeof(problem[0]), compare_structs);
	
	// Prints the sorting result
	printf("\n");
	for(i = 0; i< length_array_range;i++)
    {
          //Checking: printf("Problem: %d, Number of Possibilities: %d\n",problem[i].index,problem[i].amplitude);
          // Transforming the Index number into Text
          if (problem[i].index == 0)
             printf("Dirty Iced Evap\n");
          else if (problem[i].index == 1)
             printf("Rest TEV CAPT\n");
          else if (problem[i].index == 2)
             printf("Low Charge\n");
          else if (problem[i].index == 3)
             printf("COMP VLVS\n");
          else if (problem[i].index == 4)
             printf("Rest AFTER RECV\n");
          else if (problem[i].index == 5)
             printf("REST BEFORE RECV\n");
          else if (problem[i].index == 6)
             printf("Dirty Cond\n");
          else if (problem[i].index == 7)
             printf("Air In System\n");
          else if (problem[i].index == 8)
             printf("Over-Charge\n");	  
	}

// Keeps window open
while(true)
    ;


}


// Compare Structs Code
int compare_structs(const void *a, const void *b)
    {
        t_amplitude_index *struct_a = (t_amplitude_index *) a;
        t_amplitude_index *struct_b = (t_amplitude_index *) b;
        
        if (struct_a->amplitude < struct_b->amplitude)
        {
             return 1;
        }
        else if (struct_a->amplitude == struct_b->amplitude) 
        {
             return 0;
        }
        else 
        {
             return -1;
        }
    }

The program works quite well, thanks to all your help! I am truly grateful.

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.