can check this please

Hi everyone
I did this code, it neglagate the first 7 row, then from row 8 where the actual calculate require start.
it suppose to get the sum and avg for these column only {6,15,16,17,22,23,24,28,29,30,37,38,39,44,45,46,50,51,52,61,71,81,91,101,111,121,131,141,151,161,171,181,191 }

the code is
[code]
float sumcol(int col,string line)
{
int pos;
float sums[33] = {0.0};
// find the (col-1)the comma in the string
pos = 0;
for(int k = 0;k < line.size() && pos < (col-1); k++)
if(line[k] == ',')
pos++;
// remove the first (col-1) columns from the string
line = line.substr(pos+1);
return atof(line.c_str());

}

int main()
{

ifstream input_file("Book.csv");
string line;
int count;
float sums[33] = {0.0}, avg[]= {0.0};
float max = 0.0;
int arraysize;
int array[] = {6,15,16,17,22,23,24,28,29,30,37,38,39,44,45,46,50,51,52,61,71,81,91,101,111,121,131,141,151,161,171,181,191 };

if ( input_file.fail ())
{
cout << "Error opening file.";
}
else
{
for(int i = 0; i < 7; i++) // Scan the first 7 lines, which is not required
getline(input_file, line);
count = 8; // We are in the eighth line
while(count <= 1447) // We have to add until 1447 lines..
{
getline(input_file,line);
// number of elements in the above array
arraysize = sizeof(array) / sizeof(array[0]);
for(int col = 0; col < arraysize; col++)
{
sums[col] += sumcol(array[col],line);
}
count++; // Increment the count, to go to the next line

}
count-=8 ;
cout << count;
for(i = 0; i < arraysize; i++)
{
cout<<endl<<"The sum of all values of column " << array[i] << " is : "<<sums[i]<<endl;
avg[i] = sums[i] / count ;
cout<<endl<<"The avg of all values of column " << array[i] << " is : "<<avg[i]<<endl;
}


}

return 0;
}
[/code]
It not giving me the right result not sure why, I will attach the file ( where can I attach the file )as well to check it.
this is part of my file
[code]
SCC1 SCC2 SCC3 SCC4 SCC5 SCC6 SCC7 SCC8 SCC9 SCC10 SCC11 SCC12 SCC13 SCC14
HVD 55.07 54.98 54.98 55.07 54.98 55.07 55.04 54.98 54.98 55.07 55.01 55.07 54.98 54.98
HVD_Hys 4.89 4.78 5.01 4.94 5.01 4.94 4.92 5.01 5.04 5.08 5.11 5.01 5.08 5.06
LVD 43.98 43.98 44.02 43.98 43.98 43.98 44.02 43.95 44.02 44.02 44.02 44.02 44.02 44.02
LVD_Hys 3 3.02 3.01 3 3.02 3.01 3 3.02 3.04 2.99 3 3 3 3.03

Date(dd/mm/yyyy) Time(hh:mm:ss) ATS Status ATS Error Status ATS Battery Voltage ATS Sys DC Voltage ATS Sys AC Voltage ATS Regulated DC Voltage ATS Relay Control Input Power Status ATS InByte1 ATS InByte2 INV1 System Status INV1 Ph1 Voltage Supervisor INV1 Ph2 Voltage Supervisor INV1 Ph3 Voltage Supervisor
10/10/2004 00:00:45 190 0 13.94 345.28 200 18.46 32 3 47 6 127 228 226 228
10/10/2004 00:01:45 190 0 13.94 345.28 201.11 18.46 32 3 47 6 127 228 224 228
10/10/2004 00:02:45 190 0 13.94 345.28 200 18.46 32 3 47 6 127 228 226 228
[/code]

the sum of column no 6 is 509695059 and avg for it is 353.955 and the for column 15,16,17,.....continue as I mentioned earlier.

please help

Comments

  • [blue]
    Your [italic]sumcol[/italic] function is looking for commas in the input file but your sample file does not contain any commas in any of the lines you show. Are there really any commas?

    If your columns are seperated by whitespace (like they are in the sample provided), then you can initialize a [italic]stringstream[/italic] with the line from the file. And then read from that stream however many times you need to get to the correct column. You can then convert that value to a float. Assuming a file with lines like this:

    [code]10/10/2004 00:00:45 190 0 13.94 345.28 200[/code]

    Then a function like this:
    [code]float GetValue(int column,string line)
    {
    stringstream sstr(line);
    string temp;

    for( int i = 0; i < column; ++i )
    sstr >> temp;

    return atof(temp.c_str());
    }[/code]
    Should be all you would need and you could call it like:
    [code]GetValue(6,line);[/code]
    Which should return the value 345.38.

    If there really are columns seperated by commas, then you can replace the "[italic]sstr >> temp[/italic]" line with "[italic]getline(sstr,temp,',')[/italic]" instead.
    [/blue]
  • thank you HK_MP5KPDW
    It is a csv file so it is a comma saperated file, I did it and it gave me the right result, but a mess happend when I tried to find the min and the max for these column.
    [code]
    #include
    #include
    #include
    #include
    #include
    using namespace std;


    float GetValue(int column,string line)
    {
    stringstream sstr(line);
    string temp;

    for( int i = 0; i < column; ++i )
    getline(sstr,temp,',');

    return atof(temp.c_str());
    }

    int main()
    {

    ifstream input_file("Log_05.csv");
    string line;
    int count;
    float sums[33] = {0.0}, avg[]= {0.0}, value[]={0.0};
    float max[]={0.0}, min[] = {500.0};
    int arraysize;
    int array[] = {6,15,16,17,22,23,24,28,29,30,37,38,39,44,45,46,50,51,52,61,71,81,91,101,111,121,131,141,151,161,171,181,191};

    if ( input_file.fail ())
    {
    cout << "Error opening file.";
    }
    else
    {
    for(int i = 0; i < 7; i++) // Scan the first 7 lines, which is not required
    getline(input_file, line);
    count = 8; // We are in the eighth line
    while(count <= 1447) // We have to add until 1447 lines..
    {
    getline(input_file,line);
    // number of elements in the above array
    arraysize = sizeof(array) / sizeof(array[0]);
    for(int col = 0; col < arraysize; col++)
    {
    sums[col] += GetValue(array[col],line);
    value[col] = GetValue(array[col],line);
    [blue]if ( max[col] < value[col])
    max[col] = value[col];
    else
    min[col] = value[col];[/blue]
    }
    count++; // Increment the count, to go to the next line
    }
    count-=8 ;
    cout << count;
    for(i = 0; i < arraysize; i++)
    {
    cout<<endl<<"The sum of all values of column " << array[i] << " is : "<<sums[i]<<endl;
    avg[i] = sums[i] / count ;
    cout<<endl<<"The avg of all values of column " << array[i] << " is : "<<avg[i]<<endl;
    [blue] cout<<endl<<"The max of all values of column " << array[i] << " is : "<<max[i]<<endl;
    cout<<endl<<"The min of all values of column " << array[i] << " is : "<<min[i]<<endl;[/blue]

    }


    }

    return 0;
    }
    [/code]
    if just remove the min and max all the sum and avg will be absolutly right, but I would like to have the min and max as well.

    : [blue]
    : Your [italic]sumcol[/italic] function is looking for commas in the input file but your sample file does not contain any commas in any of the lines you show. Are there really any commas?
    :
    : If your columns are seperated by whitespace (like they are in the sample provided), then you can initialize a [italic]stringstream[/italic] with the line from the file. And then read from that stream however many times you need to get to the correct column. You can then convert that value to a float. Assuming a file with lines like this:
    :
    : [code]10/10/2004 00:00:45 190 0 13.94 345.28 200[/code]
    :
    : Then a function like this:
    : [code]float GetValue(int column,string line)
    : {
    : stringstream sstr(line);
    : string temp;
    :
    : for( int i = 0; i < column; ++i )
    : sstr >> temp;
    :
    : return atof(temp.c_str());
    : }[/code]
    : Should be all you would need and you could call it like:
    : [code]GetValue(6,line);[/code]
    : Which should return the value 345.38.
    :
    : If there really are columns seperated by commas, then you can replace the "[italic]sstr >> temp[/italic]" line with "[italic]getline(sstr,temp,',')[/italic]" instead.
    : [/blue]
    :

  • [b][red]This message was edited by HK_MP5KPDW at 2006-1-13 13:2:22[/red][/b][hr]
    : thank you HK_MP5KPDW
    : It is a csv file so it is a comma saperated file, I did it and it gave me the right result, but a mess happend when I tried to find the min and the max for these column.
    : [code]
    : #include <[red]stdio.h[/red]>
    [/code]
    [blue]That should be [italic]cstdio[/italic] instead.[/blue]


    [code]
    : #include
    : #include
    : #include
    : #include
    : using namespace std;
    :
    :
    : float GetValue(int column,string line)
    : {
    : stringstream sstr(line);
    : string temp;
    :
    : for( int i = 0; i < column; ++i )
    : getline(sstr,temp,',');
    :
    : return atof(temp.c_str());
    : }
    :
    : int main()
    : {
    :
    : ifstream input_file("Log_05.csv");
    : string line;
    : int count;
    : float sums[33] = {0.0}, [red]avg[][/red]= {0.0}, [red]value[]={0.0}[/red];
    : float [red]max[][/red]={0.0}, [red]min[][/red] = {500.0};
    [/code]
    [blue]A big problem I have with the code is that all of those arrays in red are of size 1 because of how you've declared them. This means that when you later do something like [italic]value[col][/italic] or [italic]max[i ][/italic] with [italic]col[/italic] or [italic]i[/italic] being anything other than 0 that you are accessing memory that belongs to something else. Your manner of initializing them as well might need to be looked at.[/blue]



    [code]
    : int arraysize;
    : int array[] = {6,15,16,17,22,23,24,28,29,30,37,38,39,44,
    : 45,46,50,51,52,61,71,81,91,101,111,121,131,
    : 141,151,161,171,181,191};
    :
    : if ( input_file.fail ())
    : {
    : cout << "Error opening file.";
    : }
    : else
    : {
    : for(int i = 0; i < 7; i++) // Scan the first 7 lines, which is not required
    : getline(input_file, line);
    : count = 8; // We are in the eighth line
    : while(count <= 1447) // We have to add until 1447 lines..
    : {
    : getline(input_file,line);
    : // number of elements in the above array
    : [red]arraysize[/red] = sizeof(array) / sizeof(array[0]);
    : for(int col = 0; col < arraysize; col++)
    [/code]
    [blue]The calculation for [italic]arraysize[/italic] only needs to be done once, it doesn't change so there is no need to keep doing it every iteration through the loop. I'd personally just [italic]#define[/italic] a value at the start of the program and set it to 33 and use that throughout the code... in this loop and actually for declaring the arrays at the beginning of [italic]main[/italic].[/blue]


    [code]
    : {
    : sums[col] += GetValue(array[col],line);
    : value[col] = GetValue(array[col],line);
    [/code]
    [blue]You're duplicating effort by calling the function twice to get the same value. Call the function to initialize [italic]value[col][/italic] first and then just do [italic]sums[col] += value[col];[/italic] and that will save having to needlessly call the function a second time to get the same value. NOTE: It actually looks like the [italic]value[/italic] array doesn't need to be an array at all (based on how it's being used in the program).[/blue]

    [code]
    : if ( max[col] < value[col])
    : max[col] = value[col];
    : [red]else
    : min[col] = value[col];[/red]
    : }
    [/code]
    [blue]That's not right... it needs to be [italic]if( min[col] > value[col]) min[col] = value[col];[/blue]

    [code]
    : count++; // Increment the count, to go to the next line
    : }
    : count-=8 ;
    : cout << count;
    : for(i = 0; i < arraysize; i++)
    : {
    : cout<<endl<<"The sum of all values of column " << array[i]
    : << " is : "<<sums[i]<<endl;
    : avg[i] = sums[i] / count ;
    : cout<<endl<<"The avg of all values of column " << array[i]
    : << " is : "<<avg[i]<<endl;
    : cout<<endl<<"The max of all values of column " << array[i]
    : << " is : "<<max[i]<<endl;
    : cout<<endl<<"The min of all values of column " << array[i]
    : << " is : "<<min[i]<<endl;
    :
    : }
    :
    :
    : }
    :
    : return 0;
    : }
    : [/code]
    [/italic][blue]Looks like you could probably get rid of the [italic]avg[/italic] array as well and simply do the calculation within the call to [italic]cout[/italic], i.e.: [italic]cout<<...<<sums[i ]/count<<endl;[/italic][/blue]



    : if just remove the min and max all the sum and avg will be absolutly right, but I would like to have the min and max as well.
    :



  • Thank you HK_MP5KPDW for your guidness, all my mistake comes from my lake of experience. I did all the changes, but

    1. still my min not giving me the right answer, I am going to ask a funny question because if you want to get the min value you have to initialize it to higher value like [blue] float min[33]={400.0};[/blue] the thing is it giving me a 0 and come not min values for all the column which is absolutly wrong.

    2.I made all the changes, only
    [blue]
    The calculation for arraysize only needs to be done once, it doesn't change so there is no need to keep doing it every iteration through the loop. I'd personally just #define a value at the start of the program and set it to 33 and use that throughout the code... in this loop and actually for declaring the arrays at the beginning of main.
    [/blue]
    did not get the whole idea of it.
    the code after changes
    [code]
    #include
    #include
    #include
    #include
    #include
    using namespace std;


    float GetValue(int column,string line)
    {
    stringstream sstr(line);
    string temp;

    for( int i = 0; i < column; ++i )
    getline(sstr,temp,',');

    return atof(temp.c_str());
    }

    int main()
    {

    ifstream input_file("Log_05.csv");
    string line;
    int count,arraysize;
    float sums[33] = {0.0}, max[33]={0.0},[blue] min[33] = {400.0}[/blue], value = 0.0;
    int array[] = {6,15,16,17,22,23,24,28,29,30,37,38,39,44,45,46,50,51,52,61,71,81,91,101,111,121,131,141,151,161,171,181,191};

    if ( input_file.fail ())
    {
    cout << "Error opening file.";
    }
    else
    {
    for(int i = 0; i < 7; i++) // Scan the first 7 lines, which is not required
    getline(input_file, line);
    count = 8; // We are in the eighth line
    while(count <= 1447) // We have to add until 1447 lines..
    {
    getline(input_file,line);
    // number of elements in the above array
    arraysize = sizeof(array) / sizeof(array[0]);
    for(int col = 0; col < arraysize; col++)
    {
    value = GetValue(array[col],line);
    sums[col] += value;
    if ( max[col] < value)
    max[col] = value;
    [blue]if( min[col] > value)
    min[col] = value; [/blue]

    }
    count++; // Increment the count, to go to the next line
    }
    count -= 8 ;
    cout << count;
    for(i = 0; i < arraysize; i++)
    {
    cout<<endl<<"The sum of all values of column " << array[i] << " is : "<<sums[i]<<endl;
    cout<<endl<<"The avg of all values of column " << array[i] << " is : "<<sums[i] / count<<endl;
    cout<<endl<<"The max of all values of column " << array[i] << " is : "<<max[i]<<endl;
    [blue] cout<<endl<<"The min of all values of column " << array[i] << " is : "<<min[i]<<endl;[/blue]

    }


    }

    return 0;
    }

    [/code]

    thank you for all the advise.

    : [b][red]This message was edited by HK_MP5KPDW at 2006-1-13 13:2:22[/red][/b][hr]
    : : thank you HK_MP5KPDW
    : : It is a csv file so it is a comma saperated file, I did it and it gave me the right result, but a mess happend when I tried to find the min and the max for these column.
    : : [code]
    : : #include <[red]stdio.h[/red]>
    : [/code]
    : [blue]That should be [italic]cstdio[/italic] instead.[/blue]
    :
    :
    : [code]
    : : #include
    : : #include
    : : #include
    : : #include
    : : using namespace std;
    : :
    : :
    : : float GetValue(int column,string line)
    : : {
    : : stringstream sstr(line);
    : : string temp;
    : :
    : : for( int i = 0; i < column; ++i )
    : : getline(sstr,temp,',');
    : :
    : : return atof(temp.c_str());
    : : }
    : :
    : : int main()
    : : {
    : :
    : : ifstream input_file("Log_05.csv");
    : : string line;
    : : int count;
    : : float sums[33] = {0.0}, [red]avg[][/red]= {0.0}, [red]value[]={0.0}[/red];
    : : float [red]max[][/red]={0.0}, [red]min[][/red] = {500.0};
    : [/code]
    : [blue]A big problem I have with the code is that all of those arrays in red are of size 1 because of how you've declared them. This means that when you later do something like [italic]value[col][/italic] or [italic]max[i ][/italic] with [italic]col[/italic] or [italic]i[/italic] being anything other than 0 that you are accessing memory that belongs to something else. Your manner of initializing them as well might need to be looked at.[/blue]
    :
    :
    :
    : [code]
    : : int arraysize;
    : : int array[] = {6,15,16,17,22,23,24,28,29,30,37,38,39,44,
    : : 45,46,50,51,52,61,71,81,91,101,111,121,131,
    : : 141,151,161,171,181,191};
    : :
    : : if ( input_file.fail ())
    : : {
    : : cout << "Error opening file.";
    : : }
    : : else
    : : {
    : : for(int i = 0; i < 7; i++) // Scan the first 7 lines, which is not required
    : : getline(input_file, line);
    : : count = 8; // We are in the eighth line
    : : while(count <= 1447) // We have to add until 1447 lines..
    : : {
    : : getline(input_file,line);
    : : // number of elements in the above array
    : : [red]arraysize[/red] = sizeof(array) / sizeof(array[0]);
    : : for(int col = 0; col < arraysize; col++)
    : [/code]
    : [blue]The calculation for [italic]arraysize[/italic] only needs to be done once, it doesn't change so there is no need to keep doing it every iteration through the loop. I'd personally just [italic]#define[/italic] a value at the start of the program and set it to 33 and use that throughout the code... in this loop and actually for declaring the arrays at the beginning of [italic]main[/italic].[/blue]
    :
    :
    : [code]
    : : {
    : : sums[col] += GetValue(array[col],line);
    : : value[col] = GetValue(array[col],line);
    : [/code]
    : [blue]You're duplicating effort by calling the function twice to get the same value. Call the function to initialize [italic]value[col][/italic] first and then just do [italic]sums[col] += value[col];[/italic] and that will save having to needlessly call the function a second time to get the same value. NOTE: It actually looks like the [italic]value[/italic] array doesn't need to be an array at all (based on how it's being used in the program).[/blue]
    :
    : [code]
    : : if ( max[col] < value[col])
    : : max[col] = value[col];
    : : [red]else
    : : min[col] = value[col];[/red]
    : : }
    : [/code]
    : [blue]That's not right... it needs to be [italic]if( min[col] > value[col]) min[col] = value[col];[/blue]
    :
    : [code]
    : : count++; // Increment the count, to go to the next line
    : : }
    : : count-=8 ;
    : : cout << count;
    : : for(i = 0; i < arraysize; i++)
    : : {
    : : cout<<endl<<"The sum of all values of column " << array[i]
    : : << " is : "<<sums[i]<<endl;
    : : avg[i] = sums[i] / count ;
    : : cout<<endl<<"The avg of all values of column " << array[i]
    : : << " is : "<<avg[i]<<endl;
    : : cout<<endl<<"The max of all values of column " << array[i]
    : : << " is : "<<max[i]<<endl;
    : : cout<<endl<<"The min of all values of column " << array[i]
    : : << " is : "<<min[i]<<endl;
    : :
    : : }
    : :
    : :
    : : }
    : :
    : : return 0;
    : : }
    : : [/code]
    : [/italic][blue]Looks like you could probably get rid of the [italic]avg[/italic] array as well and simply do the calculation within the call to [italic]cout[/italic], i.e.: [italic]cout<<...<<sums[i ]/count<<endl;[/italic][/blue]
    :
    :
    :
    : : if just remove the min and max all the sum and avg will be absolutly right, but I would like to have the min and max as well.
    : :
    :
    :
    :
    :

  • [b][red]This message was edited by HK_MP5KPDW at 2006-1-14 17:37:54[/red][/b][hr]
    : Thank you HK_MP5KPDW for your guidness, all my mistake comes from my lake of experience. I did all the changes, but
    :
    : 1. still my min not giving me the right answer, I am going to ask a funny question because if you want to get the min value you have to initialize it to higher value like [blue] float min[33]={400.0};[/blue] the thing is it giving me a 0 and come not min values for all the column which is absolutly wrong.

    [green]Yes, you initialize the minimum values to some high value so that things work out correctly when you start the process of checking the values you read in. Likewise, you should set the maximum values to a low value. You should try to initialize the array using a loop to be on the safe side and not just specify a single value which will likely only set the first position in the array and not the whole array like you might think it is doing.[/green]

    :
    : 2.I made all the changes, only
    : [blue]
    : The calculation for arraysize only needs to be done once, it doesn't change so there is no need to keep doing it every iteration through the loop. I'd personally just #define a value at the start of the program and set it to 33 and use that throughout the code... in this loop and actually for declaring the arrays at the beginning of main.
    : [/blue]
    : did not get the whole idea of it.

    [green]You could have moved the calculation for [italic]arraysize[/italic] outside of the loop and done the calculation once instead of each time through the loop. However, check what I've done below with the [italic]const int[/italic] value.[/green]

    [code]
    #include
    #include
    #include
    #include
    #include
    #include
    [green]#include // For numeric_limits[/green]
    using namespace std;


    float GetValue(int column,string line)
    {
    stringstream sstr(line);
    string temp;

    for( int i = 0; i < column; ++i )
    getline(sstr,temp,',');

    return atof(temp.c_str());
    }

    int main()
    {
    ifstream input_file("Log_05.csv");
    string line;
    int count, [green]loop[/green];
    int array[] = {6,15,16,17,22,23,24,28,29,30,37,38,39,44,45,
    46,50,51,52,61,71,81,91,101,111,121,131,141,
    151,161,171,181,191};
    [green]const int SIZE = sizeof array / sizeof *array;
    float sums[ SIZE ] = {0.0}, max[ SIZE ], min[ SIZE ], value;
    [/code]
    [green]Here I've made a [italic]const int[/italic] variable called [italic]SIZE[/italic] and initialized it to the size of the array. I've moved the declarations for your [italic]float[/italic] arrays below that array and the [italic]const int[/italic] variable. I then used the [italic]SIZE[/italic] variable as the parameter for declaring the number of elements in the [italic]min/max/sums[/italic] arrays. This is better since if you ever needed to change the number of items in that first [italic]int[/italic] array, then the size of the others would be adjusted automatically without you needing to do a thing. (Also added a counter variable [italic]loop[/italic] for use in a couple of the new loops.)[/green]

    [code]
    for( loop = 0; loop < SIZE; ++loop )
    max[loop] = numeric_limits::min();

    for( loop = 0; loop < SIZE; ++loop )
    min[loop] = numeric_limits::max();[/green]
    [/code]
    [green]This is some new code to initialize your [italic]min[/italic] and [italic]max[/italic] arrays. The [italic]max[/italic] gets initialized to the lowest possible [italic]float[/italic] value while the [italic]min[/italic] array gets initialized to the highest possible value.[/green]


    [code]
    if ( input_file.fail ())
    {
    cout << "Error opening file.";
    }
    else
    {
    for(int i = 0; i < 7; i++) // Scan the first 7 lines, which is not required
    getline(input_file, line);
    count = 8; // We are in the eighth line
    while(count <= 1447) // We have to add until 1447 lines..
    {
    getline(input_file,line);
    // number of elements in the above array
    for(int col = 0; col < [green]SIZE[/green]; col++)
    {
    value = GetValue(array[col],line);
    sums[col] += value;
    if( max[col] < value )
    max[col] = value;
    if( min[col] > value )
    min[col] = value;
    }
    count++; // Increment the count, to go to the next line
    }
    count -= 8;
    cout << count;
    for(i = 0; i < [green]SIZE[/green]; i++)
    {
    cout << endl << "The sum of all values of column " << array[i]
    << " is : "<<sums[i]<<endl;
    cout << endl << "The avg of all values of column " << array[i]
    << " is : "<<sums[i]/count <<endl;
    cout << endl << "The max of all values of column " << array[i]
    << " is : "<<max[i]<<endl;
    cout << endl << "The min of all values of column " << array[i]
    << " is : "<<min[i]<<endl;
    }

    }

    return 0;
    }
    [/code]
    [green]I've also made some changes to your loops removing the reference to [italic]arraysize[/italic] and replacing it with the new [italic]const int SIZE[/italic] variable. See if this helps any.[/green]


  • [blue]
    Thank you so much HK_MP5KPDW , I learn many steps from you guidness.
    thank you.
    [/blue]

    : [b][red]This message was edited by HK_MP5KPDW at 2006-1-14 17:37:54[/red][/b][hr]
    : : Thank you HK_MP5KPDW for your guidness, all my mistake comes from my lake of experience. I did all the changes, but
    : :
    : : 1. still my min not giving me the right answer, I am going to ask a funny question because if you want to get the min value you have to initialize it to higher value like [blue] float min[33]={400.0};[/blue] the thing is it giving me a 0 and come not min values for all the column which is absolutly wrong.
    :
    : [green]Yes, you initialize the minimum values to some high value so that things work out correctly when you start the process of checking the values you read in. Likewise, you should set the maximum values to a low value. You should try to initialize the array using a loop to be on the safe side and not just specify a single value which will likely only set the first position in the array and not the whole array like you might think it is doing.[/green]
    :
    : :
    : : 2.I made all the changes, only
    : : [blue]
    : : The calculation for arraysize only needs to be done once, it doesn't change so there is no need to keep doing it every iteration through the loop. I'd personally just #define a value at the start of the program and set it to 33 and use that throughout the code... in this loop and actually for declaring the arrays at the beginning of main.
    : : [/blue]
    : : did not get the whole idea of it.
    :
    : [green]You could have moved the calculation for [italic]arraysize[/italic] outside of the loop and done the calculation once instead of each time through the loop. However, check what I've done below with the [italic]const int[/italic] value.[/green]
    :
    : [code]
    : #include
    : #include
    : #include
    : #include
    : #include
    : #include
    : [green]#include // For numeric_limits[/green]
    : using namespace std;
    :
    :
    : float GetValue(int column,string line)
    : {
    : stringstream sstr(line);
    : string temp;
    :
    : for( int i = 0; i < column; ++i )
    : getline(sstr,temp,',');
    :
    : return atof(temp.c_str());
    : }
    :
    : int main()
    : {
    : ifstream input_file("Log_05.csv");
    : string line;
    : int count, [green]loop[/green];
    : int array[] = {6,15,16,17,22,23,24,28,29,30,37,38,39,44,45,
    : 46,50,51,52,61,71,81,91,101,111,121,131,141,
    : 151,161,171,181,191};
    : [green]const int SIZE = sizeof array / sizeof *array;
    : float sums[ SIZE ] = {0.0}, max[ SIZE ], min[ SIZE ], value;
    : [/code]
    : [green]Here I've made a [italic]const int[/italic] variable called [italic]SIZE[/italic] and initialized it to the size of the array. I've moved the declarations for your [italic]float[/italic] arrays below that array and the [italic]const int[/italic] variable. I then used the [italic]SIZE[/italic] variable as the parameter for declaring the number of elements in the [italic]min/max/sums[/italic] arrays. This is better since if you ever needed to change the number of items in that first [italic]int[/italic] array, then the size of the others would be adjusted automatically without you needing to do a thing. (Also added a counter variable [italic]loop[/italic] for use in a couple of the new loops.)[/green]
    :
    : [code]
    : for( loop = 0; loop < SIZE; ++loop )
    : max[loop] = numeric_limits::min();
    :
    : for( loop = 0; loop < SIZE; ++loop )
    : min[loop] = numeric_limits::max();[/green]
    : [/code]
    : [green]This is some new code to initialize your [italic]min[/italic] and [italic]max[/italic] arrays. The [italic]max[/italic] gets initialized to the lowest possible [italic]float[/italic] value while the [italic]min[/italic] array gets initialized to the highest possible value.[/green]
    :
    :
    : [code]
    : if ( input_file.fail ())
    : {
    : cout << "Error opening file.";
    : }
    : else
    : {
    : for(int i = 0; i < 7; i++) // Scan the first 7 lines, which is not required
    : getline(input_file, line);
    : count = 8; // We are in the eighth line
    : while(count <= 1447) // We have to add until 1447 lines..
    : {
    : getline(input_file,line);
    : // number of elements in the above array
    : for(int col = 0; col < [green]SIZE[/green]; col++)
    : {
    : value = GetValue(array[col],line);
    : sums[col] += value;
    : if( max[col] < value )
    : max[col] = value;
    : if( min[col] > value )
    : min[col] = value;
    : }
    : count++; // Increment the count, to go to the next line
    : }
    : count -= 8;
    : cout << count;
    : for(i = 0; i < [green]SIZE[/green]; i++)
    : {
    : cout << endl << "The sum of all values of column " << array[i]
    : << " is : "<<sums[i]<<endl;
    : cout << endl << "The avg of all values of column " << array[i]
    : << " is : "<<sums[i]/count <<endl;
    : cout << endl << "The max of all values of column " << array[i]
    : << " is : "<<max[i]<<endl;
    : cout << endl << "The min of all values of column " << array[i]
    : << " is : "<<min[i]<<endl;
    : }
    :
    : }
    :
    : return 0;
    : }
    : [/code]
    : [green]I've also made some changes to your loops removing the reference to [italic]arraysize[/italic] and replacing it with the new [italic]const int SIZE[/italic] variable. See if this helps any.[/green]
    :
    :
    :

  • Hi HK_MP5KPDW
    I have one final question
    I building my program in MVC++ using Console application, my code do not have any interface, I printing my output in an output file.
    one thing I would like to ask, when running the code is there is any way to stop the black screen ( window prompt from not appear) when execuate the code.
    I used
    [code]
    #include
    int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
    [/code]
    but the probelem it giving me error when I added it to my code
    [code]
    warning C4003: not enough actual parameters for macro 'min'
    error C2589: '(' : illegal token on right side of '::'
    error C2059: syntax error : '::'
    : warning C4003: not enough actual parameters for macro 'max'
    error C2589: '(' : illegal token on right side of '::'
    error C2059: syntax error : '::'
    [/code]
    this error related to the statement
    [code]
    for( loop = 0; loop < SIZE; ++loop )
    max[loop] = numeric_limits::min();

    for( loop = 0; loop < SIZE; ++loop )
    min[loop] = numeric_limits::max();
    [/code]
    but I want this code as it helped so much in the min and max and I am not sure how to fix that error. to be honest I do not have any idea why is this.

    thank you for all the help



    : [b][red]This message was edited by HK_MP5KPDW at 2006-1-14 17:37:54[/red][/b][hr]
    : : Thank you HK_MP5KPDW for your guidness, all my mistake comes from my lake of experience. I did all the changes, but
    : :
    : : 1. still my min not giving me the right answer, I am going to ask a funny question because if you want to get the min value you have to initialize it to higher value like [blue] float min[33]={400.0};[/blue] the thing is it giving me a 0 and come not min values for all the column which is absolutly wrong.
    :
    : [green]Yes, you initialize the minimum values to some high value so that things work out correctly when you start the process of checking the values you read in. Likewise, you should set the maximum values to a low value. You should try to initialize the array using a loop to be on the safe side and not just specify a single value which will likely only set the first position in the array and not the whole array like you might think it is doing.[/green]
    :
    : :
    : : 2.I made all the changes, only
    : : [blue]
    : : The calculation for arraysize only needs to be done once, it doesn't change so there is no need to keep doing it every iteration through the loop. I'd personally just #define a value at the start of the program and set it to 33 and use that throughout the code... in this loop and actually for declaring the arrays at the beginning of main.
    : : [/blue]
    : : did not get the whole idea of it.
    :
    : [green]You could have moved the calculation for [italic]arraysize[/italic] outside of the loop and done the calculation once instead of each time through the loop. However, check what I've done below with the [italic]const int[/italic] value.[/green]
    :
    : [code]
    : #include
    : #include
    : #include
    : #include
    : #include
    : #include
    : [green]#include // For numeric_limits[/green]
    : using namespace std;
    :
    :
    : float GetValue(int column,string line)
    : {
    : stringstream sstr(line);
    : string temp;
    :
    : for( int i = 0; i < column; ++i )
    : getline(sstr,temp,',');
    :
    : return atof(temp.c_str());
    : }
    :
    : int main()
    : {
    : ifstream input_file("Log_05.csv");
    : string line;
    : int count, [green]loop[/green];
    : int array[] = {6,15,16,17,22,23,24,28,29,30,37,38,39,44,45,
    : 46,50,51,52,61,71,81,91,101,111,121,131,141,
    : 151,161,171,181,191};
    : [green]const int SIZE = sizeof array / sizeof *array;
    : float sums[ SIZE ] = {0.0}, max[ SIZE ], min[ SIZE ], value;
    : [/code]
    : [green]Here I've made a [italic]const int[/italic] variable called [italic]SIZE[/italic] and initialized it to the size of the array. I've moved the declarations for your [italic]float[/italic] arrays below that array and the [italic]const int[/italic] variable. I then used the [italic]SIZE[/italic] variable as the parameter for declaring the number of elements in the [italic]min/max/sums[/italic] arrays. This is better since if you ever needed to change the number of items in that first [italic]int[/italic] array, then the size of the others would be adjusted automatically without you needing to do a thing. (Also added a counter variable [italic]loop[/italic] for use in a couple of the new loops.)[/green]
    :
    : [code]
    : for( loop = 0; loop < SIZE; ++loop )
    : max[loop] = numeric_limits::min();
    :
    : for( loop = 0; loop < SIZE; ++loop )
    : min[loop] = numeric_limits::max();[/green]
    : [/code]
    : [green]This is some new code to initialize your [italic]min[/italic] and [italic]max[/italic] arrays. The [italic]max[/italic] gets initialized to the lowest possible [italic]float[/italic] value while the [italic]min[/italic] array gets initialized to the highest possible value.[/green]
    :
    :
    : [code]
    : if ( input_file.fail ())
    : {
    : cout << "Error opening file.";
    : }
    : else
    : {
    : for(int i = 0; i < 7; i++) // Scan the first 7 lines, which is not required
    : getline(input_file, line);
    : count = 8; // We are in the eighth line
    : while(count <= 1447) // We have to add until 1447 lines..
    : {
    : getline(input_file,line);
    : // number of elements in the above array
    : for(int col = 0; col < [green]SIZE[/green]; col++)
    : {
    : value = GetValue(array[col],line);
    : sums[col] += value;
    : if( max[col] < value )
    : max[col] = value;
    : if( min[col] > value )
    : min[col] = value;
    : }
    : count++; // Increment the count, to go to the next line
    : }
    : count -= 8;
    : cout << count;
    : for(i = 0; i < [green]SIZE[/green]; i++)
    : {
    : cout << endl << "The sum of all values of column " << array[i]
    : << " is : "<<sums[i]<<endl;
    : cout << endl << "The avg of all values of column " << array[i]
    : << " is : "<<sums[i]/count <<endl;
    : cout << endl << "The max of all values of column " << array[i]
    : << " is : "<<max[i]<<endl;
    : cout << endl << "The min of all values of column " << array[i]
    : << " is : "<<min[i]<<endl;
    : }
    :
    : }
    :
    : return 0;
    : }
    : [/code]
    : [green]I've also made some changes to your loops removing the reference to [italic]arraysize[/italic] and replacing it with the new [italic]const int SIZE[/italic] variable. See if this helps any.[/green]
    :
    :
    :

  • [b][red]This message was edited by HK_MP5KPDW at 2006-1-16 12:28:32[/red][/b][hr]
    : Hi HK_MP5KPDW
    : I have one final question
    : I building my program in MVC++ using Console application, my code do not have any interface, I printing my output in an output file.
    : one thing I would like to ask, when running the code is there is any way to stop the black screen ( window prompt from not appear) when execuate the code.
    : I used
    : [code]
    : #include
    : int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
    : [/code]
    : but the probelem it giving me error when I added it to my code
    : [code]
    : warning C4003: not enough actual parameters for macro 'min'
    : error C2589: '(' : illegal token on right side of '::'
    : error C2059: syntax error : '::'
    : : warning C4003: not enough actual parameters for macro 'max'
    : error C2589: '(' : illegal token on right side of '::'
    : error C2059: syntax error : '::'
    : [/code]
    : this error related to the statement
    : [code]
    : for( loop = 0; loop < SIZE; ++loop )
    : max[loop] = numeric_limits::min();
    :
    : for( loop = 0; loop < SIZE; ++loop )
    : min[loop] = numeric_limits::max();
    : [/code]
    : but I want this code as it helped so much in the min and max and I am not sure how to fix that error. to be honest I do not have any idea why is this.
    :
    : thank you for all the help
    :
    :
    :

    [blue]If you're making a console application, then you should be using [italic]main[/italic] and not [italic]WinMain[/italic]. [italic]WinMain[/italic] is for a Windows Applications. The problem with [italic]min/max[/italic] macros are that the [italic]windows.h[/italic] header file includes some stuff which defines a [italic]min[/italic] and [italic]max[/italic] macros that interferes with that code. You shouldn't need that header if you are just making a console application. However, if you really think you need it, you could try putting [purple]this[/purple] #define before you include the header to fix that:
    [code]
    [purple]#define NOMINMAX[/purple]
    #include
    [/code][/blue]
  • Thank you HK_MP5KPDW
    I would like to ask small question,
    [red]some values[/red] for sum column give my out put like this 113384e+006 can I change it to 1133841 because sometime it is confusing to the people when they read it.

    thank again for all your advise.

    : [b][red]This message was edited by HK_MP5KPDW at 2006-1-16 12:28:32[/red][/b][hr]
    : : Hi HK_MP5KPDW
    : : I have one final question
    : : I building my program in MVC++ using Console application, my code do not have any interface, I printing my output in an output file.
    : : one thing I would like to ask, when running the code is there is any way to stop the black screen ( window prompt from not appear) when execuate the code.
    : : I used
    : : [code]
    : : #include
    : : int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
    : : [/code]
    : : but the probelem it giving me error when I added it to my code
    : : [code]
    : : warning C4003: not enough actual parameters for macro 'min'
    : : error C2589: '(' : illegal token on right side of '::'
    : : error C2059: syntax error : '::'
    : : : warning C4003: not enough actual parameters for macro 'max'
    : : error C2589: '(' : illegal token on right side of '::'
    : : error C2059: syntax error : '::'
    : : [/code]
    : : this error related to the statement
    : : [code]
    : : for( loop = 0; loop < SIZE; ++loop )
    : : max[loop] = numeric_limits::min();
    : :
    : : for( loop = 0; loop < SIZE; ++loop )
    : : min[loop] = numeric_limits::max();
    : : [/code]
    : : but I want this code as it helped so much in the min and max and I am not sure how to fix that error. to be honest I do not have any idea why is this.
    : :
    : : thank you for all the help
    : :
    : :
    : :
    :
    : [blue]If you're making a console application, then you should be using [italic]main[/italic] and not [italic]WinMain[/italic]. [italic]WinMain[/italic] is for a Windows Applications. The problem with [italic]min/max[/italic] macros are that the [italic]windows.h[/italic] header file includes some stuff which defines a [italic]min[/italic] and [italic]max[/italic] macros that interferes with that code. You shouldn't need that header if you are just making a console application. However, if you really think you need it, you could try putting [purple]this[/purple] #define before you include the header to fix that:
    : [code]
    : [purple]#define NOMINMAX[/purple]
    : #include
    : [/code][/blue]
    :

  • : Thank you HK_MP5KPDW
    : I would like to ask small question,
    : [red]some values[/red] for sum column give my out put like this 113384e+006 can I change it to 1133841 because sometime it is confusing to the people when they read it.
    :
    : thank again for all your advise.
    :


    [green]You can try this new stuff in [blue]blue[/blue]:
    [code]cout << endl << "The sum of all values of column " << [blue]fixed <<[/blue] array[i]
    << " is : " << sums[i] << endl;
    [/code]
    See if that works.[/green]
  • Hi

    using [blue]fixed[/blue] in a way help in get red of [red]e[/red], but it is scarey
    using fixed
    [code]
    The total Voltage in Hours is: 14031188.000000

    The total Voltage in Minutes is: 841702939.412117
    [/code]
    without fixed
    [code]
    The total Voltage in Hours is: 1.40312e+007

    The total Voltage in Minutes is: 8.41703e+008
    [/code]
    now I am confuse which is more understandable.

    by the way the above [red] total voltage in hour [/red] I suppose to divided by 1/60 to find the voltage in minutes so what I did is like
    [code]
    cout << endl << " The total Voltage in Minutes is: " <<[red] total_v/(0.01667)[/red] << endl;
    [/code]
    because if I write like
    [code]
    cout << endl << " The total In Watt in Minutes is: " << [red]total_v/(1/60)[/red] << endl;
    [/code]
    it give me 0 result. is what I did right.







    : : Thank you HK_MP5KPDW
    : : I would like to ask small question,
    : : [red]some values[/red] for sum column give my out put like this 113384e+006 can I change it to 1133841 because sometime it is confusing to the people when they read it.
    : :
    : : thank again for all your advise.
    : :
    :
    :
    : [green]You can try this new stuff in [blue]blue[/blue]:
    : [code]cout << endl << "The sum of all values of column " << [blue]fixed <<[/blue] array[i]
    : << " is : " << sums[i] << endl;
    : [/code]
    : See if that works.[/green]
    :

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories