Welcome to Dream.In.Code
Become a C++ Expert!

Join 149,570 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,658 people online right now. Registration is fast and FREE... Join Now!




converting program to use vectors

 
Reply to this topicStart new topic

converting program to use vectors, Help C++

mitch_bienusa
30 Oct, 2007 - 11:28 AM
Post #1

New D.I.C Head
*

Joined: 30 Oct, 2007
Posts: 1


My Contributions
ok... I have spent a while making this program.. about a week and at the end of the week my instructor wants me to convert it to vectors and i have no idea.. any ideas??


CODE

#include <iostream>
#include<math.h>
#include<fstream>
float find_mean(float how_many, float list[]);
float find_variance(int how_many, float list[]);
float find_max( float sorted_list[], int how_many);
float find_min(float sorted_list[], int how_many);
float find_svariance(int how_many, float list[]);
float find_deviation(int how_many, float list[]);
float find_sample_standard_deviation(int how_many, float list[]);
float find_median(float sorted_list[], int how_many);
float find_q1(float sorted_list[], int how_many);
float find_q3(float sorted_list[], int how_many);
float find_iqr(float sorted_list[], int how_many);
float find_lower(float sorted_list[], int how_many);
float find_upper(float sorted_list[], int how_many);
void sort_list(float list[],int how_many,float sorted_list[]);
float find_highest(float sorted_list[], int how_many);
float find_lowest(float sorted_list[], int how_many);
void find_mode(float sorted_list[], int how_many);
void output(float mean, float variance, int how_many, float svariance,float deviation, float sample_standard_deviation, float max, float min, float median, float q3, float q3, float iqr, float lower, float upper, float highest, float lowest);
using namespace std;
int main(void)

{
char TextFileName[1400];
int how_many;

float mean, variance,svariance, deviation,sample_standard_deviation;
cout<<"\t Enter the name of your file include the extention"<<endl;
cin>>TextFileName;
ifstream infile;
infile.open(TextFileName,ios::in);
infile>>how_many;
float list[how_many];
float sorted_list[how_many];
float max, min, median, q1,q3, iqr;
if(infile.fail())
{
cout<<"\t check your file it cannot be opened"<<endl;
}
for(int i=0;i<how_many;i++)
{
infile>>list[i];
}
float lower, upper, lowest , highest, mode;

mean=find_mean(how_many,list);
variance=find_variance(how_many,list);
svariance=find_svariance(how_many,list);
deviation=find_deviation(how_many, list);
sample_standard_deviation= find_sample_standard_deviation(how_many, list);
sort_list(list, how_many, sorted_list);
median= find_median(sorted_list, how_many);
max=find_max(sorted_list,how_many);
min=find_min(sorted_list,how_many);
q1=find_q1(sorted_list,how_many);
q3=find_q3(sorted_list, how_many);
iqr=find_iqr(sorted_list, how_many);
lower= find_lower( sorted_list,  how_many);
upper= find_upper(sorted_list, how_many);
highest= find_highest(sorted_list, how_many);
lowest= find_lowest(sorted_list, how_many);
find_mode(sorted_list, how_many);
output(mean, variance, how_many, svariance, deviation, sample_standard_deviation, max, min, median, q1, q3, iqr, lower, upper, highest, lowest);
infile.close();




cout <<"\n\n\tWhat is the name of your output file. ";
cin >> TextFileName;
ofstream datafile;
datafile.open (TextFileName,ios::out);
if (datafile.fail())
{
cerr <<"\tSTATUS: Word file ..... "<<TextFileName<<" ...could not be opened."<<endl;
exit (1);
}
datafile<<"mean="<<mean<<endl;
datafile<<"variance="<<variance<<endl;
datafile<<"how_many="<<how_many<<endl;
datafile<<"svariance="<<svariance<<endl;
datafile<<"deviation="<<deviation<<endl;
datafile<<"sample standard deviation="<<sample_standard_deviation<<endl;
datafile<<"max="<<max<<endl;
datafile<<"min="<<min<<endl;
datafile<<"median="<<median<<endl;
datafile<<"q1="<<q1<<endl;
datafile<<"q3="<<q3<<endl;
datafile<<"iqr="<<iqr<<endl;
datafile<<"Major lower outlier="<<lower<<endl;
datafile<<"Minor upper outlier="<<upper<<endl;
datafile<<"Major upper outlier="<<highest<<endl;
datafile<<"Minor lower outlier="<<lowest<<endl;
datafile<<"Mode is ="<<mode<<endl;
datafile.close();







return(0);
}




float find_mean(float how_many, float list[])
{
float mean;
float sum;
sum=0;
for(int i=0;i<how_many;i++)
{
sum= sum+list[i];
}
mean=sum/how_many;
return(mean);
}



float find_variance(int how_many, float list[])
{
float mean=find_mean(how_many,list);
float r_list[how_many];
for (int y=0; y<how_many; y++)
{
r_list[y]=list[y]-mean;
}
float sum=0;
for(int y=0; y<how_many;y++)
{
sum=sum+(pow(r_list[y],2));
}
float variance= sum/how_many;
return(variance);
}




float find_svariance(int how_many, float list[])
{
float variance= find_variance(how_many, list);
float svariance= variance*how_many/(how_many-1);
return(svariance);
}




float find_deviation(int how_many, float list[])
{
float variance= find_variance(how_many,list);
float deviation= sqrt(variance);
return(deviation);
}



float find_sample_standard_deviation(int how_many, float list[])
{
float svariance=find_svariance(how_many,list);
float sample_standard_deviation= sqrt(svariance);
return(sample_standard_deviation);
}




void sort_list(float list[],int how_many,float sorted_list[])
{

    for(int v=0; v<how_many; v++)
    {
    sorted_list[v]=list[v];
    }


int temp;
    for( int i=0; i<how_many-1; i++)
    {
    


        for(int  j=i; j<how_many-1; j++)
        {
            if (sorted_list[j+1]<sorted_list[i])
            {
            temp= sorted_list[i];
            sorted_list[i] = sorted_list[j+1];
            sorted_list[j+1]= temp;
            }
        }
    }
for (int s=0; s<how_many; s++)
{
cout<<"Number"<<(s+1)<<"in the list="<<sorted_list[s]<<"."<<endl;
}
}


float find_median(float sorted_list[],int how_many)
{
float median;
float x,y;


if(how_many%2==0)
    {
    
    x=(sorted_list[how_many/2]);
    y=(sorted_list[(how_many/2)-1]);
    median = (x+y)/2;
    }
    else
    {
median= sorted_list[how_many/2];
    }
    

return median;
}




float find_max (float sorted_list[], int how_many)
{
float max;
max=sorted_list[how_many-1];
return max;
}


float find_min (float sorted_list[], int how_many)
{
float min;
min=sorted_list[0];
return min;
}


float find_q1(float sorted_list[], int how_many)
{
float q1, x, y;
if((how_many/2)%2==0)
    {
    x=(sorted_list[how_many/4]);
    y=(sorted_list[(how_many/4)-1]);
    q1= (x+y)/2;
    }
else
{
q1= sorted_list[how_many/4];
}

return(q1);
}



float find_q3(float sorted_list[], int how_many)
{
float q3, x,y;
if (how_many % 2 == 0)
               {

               if((how_many / 2) % 2 == 0)
                       {
                       x = sorted_list[(how_many * 3) / 4];
                       y = sorted_list[((how_many * 3) / 4) - 1];
                       q3 = (x + y) / 2;
                       }

               if ((how_many / 2) % 2 != 0)
                       {
                       q3 = sorted_list[(how_many * 3) / 4];
                       }
               }

       if(how_many % 2 != 0)
               {

               if((how_many / 2) % 2 == 0)
                       {
                       x = sorted_list[(how_many * 3) / 4];
                       y = sorted_list[((how_many * 3) / 4) + 1];
                       q3 = (x + y) / 2;
                       }

               if ((how_many / 2) % 2 != 0)
                       {
                       q3 = sorted_list[(how_many * 3) / 4];
                       }
               }
return(q3);
}


float find_iqr(float sorted_list[], int how_many)
{
float iqr;
float q1= find_q1(sorted_list, how_many);
float q3= find_q3(sorted_list, how_many);
iqr= (q3-q1);
return(iqr);

}
void output(float mean, float variance, int how_many, float svariance,float deviation, float sample_standard_deviation, float max, float min, float median, float q1, float q3, float iqr, float lower, float upper, float highest, float lowest)
{
cout<<"\n The number of numbers is\t"<<how_many;
cout<<"\n The mean is\t"<<mean<<endl;
cout<<"\n The population variance is\t"<<variance<<endl;
cout<<"\n The sample variance is\t"<<svariance<<endl;
cout<<"\n The deviation is\t"<<deviation<<endl;
cout<<"\n The sample standard deviation is \t"<<sample_standard_deviation<<endl;
cout<<"\n The max is\t"<<max<<endl;
cout<<"\n The min is\t"<<min<<endl;
cout<<"\n q3 is\t"<<q3<<endl;
cout<<"\n q1 is\t"<<q1<<endl;
cout<<"\n The iqr is\t"<<iqr<<endl;
cout<<"\n the median is\t"<<median<<endl;
cout<<"\n The Major lower outlier is\t"<<lower<<endl;
cout<<"\n The Minor upper is\t"<<upper<<endl;
cout<<"\n The Major upper Outlier is\t"<<highest<<endl;
cout<<"\n The Minor lower outlier is\t"<<lowest<<endl;
}



float find_lower(float sorted_list[], int how_many)
{

float q3 = find_q3 (sorted_list, how_many);
      
       float iqr = find_iqr (sorted_list, how_many);
       float lower;

       lower = q3 + 1.5 * iqr;

       return lower;


}




float find_upper(float sorted_list[], int how_many)
{



       float q1 = find_q1 (sorted_list, how_many);
       float iqr = find_iqr (sorted_list, how_many);
       float upper;

       upper = q1 - 3.0 * iqr;

       return upper;

}



float find_highest(float sorted_list[], int how_many)
{
  float q3 = find_q3 (sorted_list, how_many);
    
       float iqr = find_iqr (sorted_list, how_many);
       float highest;

       highest = q3 + 3.0 * iqr;

       return highest;


}




float find_lowest(float sorted_list[], int how_many)

{
      
       float q1 = find_q1 (sorted_list, how_many);
       float iqr = find_iqr (sorted_list, how_many);
       float lowest;

       lowest = q1 - 1.5 * iqr;

       return lowest;


}


void find_mode(float sorted_list[], int how_many)
{
int c_list[how_many];

int c_max=c_list[0];

for( int a=0; a<how_many; a++)
{
c_list[a]=1;
}
for(int b=1; b<how_many; b++)
{
if(sorted_list[b]==sorted_list[b-1])
{
c_list[b]= c_list[b]+ c_list[b-1];
}
}

for(int c=0;c< how_many; c++)
{
if(c_list[c]>c_max)
{
c_max= c_list[c];
}
}


int n_mode=0;
for(int d=0; d<how_many; d++)
{
if(c_list[d]==c_max)
{

cout<<" mode is:"<<sorted_list[d]<<endl;
n_mode+=1;

}
}

if(n_mode== how_many)
{

n_mode=0;
c_max=0;
}
cout<<"\t there are "<<n_mode<<"modes"<<endl;
cout<<"\t the frequency of the modes is:"<<c_max<<endl;
}

User is offlineProfile CardPM
+Quote Post

William_Wilson
RE: Converting Program To Use Vectors
30 Oct, 2007 - 11:34 AM
Post #2

lost in compilation
Group Icon

Joined: 23 Dec, 2005
Posts: 4,101



Thanked: 25 times
Dream Kudos: 3275
Expert In: Java, C, Javascript

My Contributions
code.gif next time please
User is offlineProfile CardPM
+Quote Post

jjhaag
RE: Converting Program To Use Vectors
30 Oct, 2007 - 11:42 AM
Post #3

me editor am smartastic
Group Icon

Joined: 18 Sep, 2007
Posts: 1,789



Thanked: 2 times
Dream Kudos: 775
Expert In: C,C++

My Contributions
Presumably this means that you're supposed to use STL vectors instead of raw arrays. I would start by taking a look at these two tutorials:
Vectors I
Vectors II

I would also recommend that you start small with this. Start a new project, take your input steps and the first function that you use, and try to do it using vectors - don't try to do your entire program all at once. If you make a mistake in 1 place, you're liable to do it every place that you're performing similar operations, and that will just get frustrating. And since many of the function calls are very similar, once you've figured out how to convert one of your functions, the rest should be pretty easy to do.

Hope that helps get you started,

-jjh
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 1/7/09 10:21PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month