Jump to content

I need help on a program for my data structures class

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
14 replies to this topic

#1
noyfb

noyfb

    Newbie

  • Members
  • Pip
  • 7 posts
Ok so far I have received no help with this whatsoever so this is my last resort :irritated:. Two weeks ago I was given the following assignment:

o Input range of N values

o Create range_time_vector

o Input number_of_cases

o for each input in the range of inputs

· Set time_sum to zero

· Set time_average to zero

· For each case

§ Start the time

§ Execute the loop

§ End the time

§ Add the time difference to time_sum

o Calculate time_average = time_sum/ number_of_cases

o Store time_average in range_time_vector at input index

when I got to the point to "start the time" I just couldn't understand it. My previous programming classes ever did anything like this, so I have no idea what to do. Below is what I've been able to do so far:

#include <iostream>
#include <vector>

using namespace std;

int main ()
{
      int N =0;
      int Range=0;
      int NumberOfCases=0;

      cout << "Enter N" <<endl;
      cin >> N;

      cout << "Enter Range" <<endl;
      cin >> Range;

      cout << "Number of cases" <<endl;
      cin >> NumberOfCases;

      vector<double> range_time_Vector(Range);
      int K = N;

      for(int i=0; i < Range; ++i)
      {
          double time_sum = 0.0;
          double time_average = 0.0;
          for (int m=1 ; m <= NumberOfCases; ++m)
          {
             cout << "for m=" << m << "  K=" << K << endl;
          }
      }
      return 0;
}

Is there anyone that can help me with this: My tutor wasn't even able to understand the directions, so I wasn't able to get help that much. Thanks!

Edited by ZekeDragon, 12 March 2010 - 04:06 PM.
Please use [code] tags.


#2
kmhosny

kmhosny

    Programmer

  • Members
  • PipPipPipPip
  • 133 posts
is this algorithm to compute the time it takes for the inner loop?
if so you can use the ctime header file to get the time before the inner loop -start time- and then get the time after the inner loop -end time- and subtract the end time from the start you will get the loop time -i think it will be the time sum- and get the average using the equation in the algorithm.

#include <iostream>
#include <vector>
#include <ctime>//must include ctime
using namespace std;
int main ()
{
      int N =0;
      int Range=0;
      int NumberOfCases=0;

      cout << "Enter N" <<endl;
      cin >> N;

      cout << "Enter Range" <<endl;
      cin >> Range;

      cout << "Number of cases" <<endl;
      cin >> NumberOfCases;

      vector<double> range_time_Vector(Range);
      int K = N;

      for(int i=0; i < Range; ++i)
      {
          double time_sum = 0.0;
          double time_average = 0.0;
//get the time at this position-start_time-
          for (int m=1 ; m <= NumberOfCases; ++m)
          {
          }
//get the time at this position -end time-
//get the difference in between start and end and assign it to time_sum
//get the average and add it to the vector
      }
      return 0;
}
i hope i helped.

EDIT: you can get the time by
time_t t1=time(0);

Edited by kmhosny, 10 March 2010 - 02:33 PM.
add edit

"Recursion is just a line of code"
-Karim Hosny-
My flickr

#3
noyfb

noyfb

    Newbie

  • Members
  • Pip
  • 7 posts
So for:
//get the time at this position-start_time-

would that be:
st->startTimer();

and would:
//get the time at this position -end time-

be:
st->stopTimer();

#4
kmhosny

kmhosny

    Programmer

  • Members
  • PipPipPipPip
  • 133 posts
for start time : time_t start_time=time(0);
for end time : time_t end_time=time(0);
"Recursion is just a line of code"
-Karim Hosny-
My flickr

#5
noyfb

noyfb

    Newbie

  • Members
  • Pip
  • 7 posts
I just tried the following program and for some reason the program didn't time how long it took for the program to come up with the solutions:

#include <iostream>
#include <vector>
#include <ctime> //must include ctime

using namespace std;

int main ()

{
int N =0;
int Range=0;
int NumberOfCases=0;

cout << "Enter N" <<endl;
cin >> N;

cout << "Enter Range" <<endl;
cin >> Range;

cout << "Number of cases" <<endl;
cin >> NumberOfCases;

vector<double> range_time_Vector(Range);

int K = N;

for(int i=0; i < Range; ++i)

{
double time_sum = 0.0;
double time_average = 0.0;

//get the time at this position –start_time-
time_t start_time=time(0);

for (int m=1 ; m <= NumberOfCases; ++m)
{

cout << "for m=" << m << " K=" << K << endl;
}
}
//get the time at this position –end_time-
time_t end_time=time(0);

//get the difference in between start and end and assign it to time_sum
//get the average and add it to the vector

return 0;
}

I didn't add the other functions because 1 I don't know how to and 2 I just wanted to see whether or not the timing code was going to work. Can you help me figure out what's wrong with it?

Edited by ZekeDragon, 12 March 2010 - 04:05 PM.
Please use [code] tags


#6
kmhosny

kmhosny

    Programmer

  • Members
  • PipPipPipPip
  • 133 posts
well i the code you just posted it did you used the time(0) function what this function actually does is getting the current time in seconds, so calculating the current time at the start and calculating it again at the end and subtracting them will give you the amount of time it took for executing the code in between the 2 function calls -time(0) at the start and time(0) at the end.
so you have called it to get the time but you didnt subtract the values what is missing is:
time_sum=end_time - start_time;//get the difference in between start and end and assign it to time_sum
time_average=time_sum/number_of_cases;//get the average 
range_time_vector[i]=time_average;//and add it to the vector
note:when posting a code place it between code tags"write code in capital letters between [] and [/] at the end of the code" or you can just press the # button in the reply area
"Recursion is just a line of code"
-Karim Hosny-
My flickr

#7
noyfb

noyfb

    Newbie

  • Members
  • Pip
  • 7 posts
Thanks! Since I'm at work, I won't be able to try the code until 5pm Central. I'll let you know how it went. I really appreciate you helping me.

#8
abdul.gafur

abdul.gafur

    Learning Programmer

  • Members
  • PipPipPip
  • 42 posts
#include <iostream>

#include <vector>

#include <ctime> //must include ctime

#include <conio.h>


using namespace std;


int main () 

{

    time_t start,end;

    int N =0;

    int Range=0;

    int NumberOfCases=0;

    double time_sum = 0.0;

    double time_average = 0.0;

    

    cout << "Enter N" <<endl;

    cin >> N;

    

    cout << "Enter Range" <<endl;

    cin >> Range;

    

    cout << "Number of cases" <<endl;

    cin >> NumberOfCases;

    

    vector<double> range_time_Vector(Range);

    

    int K = N;

    

    //get the time at this position –start_time-

    time(&start);

    clock_t t1=clock();

    

    for(int i=0; i < Range; ++i) {

        for (int m=1 ; m <= NumberOfCases; ++m) {           

        }

    }

    

    //get the time at this position –end_time-

    time(&end);

    clock_t t2=clock();

    

    //get the difference in between start and end and assign it to time_sum

    time_sum = double(t2-t1)/CLOCKS_PER_SEC;

    time_average = time_sum/NumberOfCases;

    cout<<"time sum (using clock) : "<<time_sum<<endl;

    cout<<"time average (using clock) : "<<time_average<<endl;

    

    time_sum = (double)difftime(end,start);

    time_average = time_sum/NumberOfCases;

    cout<<"time sum (using difftime) : "<<time_sum<<endl;

    cout<<"time average (using difftime) : "<<time_average<<endl;

    

    

    //get the average and add it to the vector

    

    // I don't know how to assign value to vector variable


    getch();

    return 0;

}


#9
noyfb

noyfb

    Newbie

  • Members
  • Pip
  • 7 posts
Here's the output:

Enter N
50
Enter Range
55
Number of cases
25
time sum (using clock) : 0
time average (using clock) : 0
time sum (using difftime) : 0
time average (using difftime) : 0

For some reason its not actually telling me how long in took, in seconds for the program to come up with the solutions. Its not even showing what k or m equals. Its like its been completely ignored:confused:

#10
noyfb

noyfb

    Newbie

  • Members
  • Pip
  • 7 posts
I just tried to build a solution and here's what its saying:

1>------ Rebuild All started: Project: Timing Project, Configuration: Debug Win32 ------
1>Deleting intermediate and output files for project 'Timing Project', configuration 'Debug|Win32'
1>Compiling...
1>Timing Project.cpp
1>c:\[path] : error C2065: 'time_sum' : undeclared identifier
1>c:\[path] : error C2065: 'start_time' : undeclared identifier
1>c:\[path]: error C2065: 'time_average' : undeclared identifier
1>c:\[path]: error C2065: 'time_sum' : undeclared identifier
1>c:\[path]: error C2065: 'number_of_cases' : undeclared identifier
1>c:\[path] : error C2065: 'range_time_vector' : undeclared identifier
1>c:\[path] : error C2065: 'i' : undeclared identifier
1>c:\[path] : error C2065: 'time_average' : undeclared identifier
1>Build log was saved at "file://c:\[file]"
1>Timing Project - 8 error(s), 0 warning(s)
========== Rebuild All: 0 succeeded, 1 failed, 0 skipped ==========

#11
abdul.gafur

abdul.gafur

    Learning Programmer

  • Members
  • PipPipPip
  • 42 posts
#include <iostream>

#include <vector>

#include <ctime> //must include ctime

#include <conio.h>


using namespace std;


int main () 

{

    time_t start,end;

    int N =0;

    int Range=0;

    int NumberOfCases=0;

    double time_sum = 0.0;

    double time_average = 0.0;

    

    cout << "Enter N" <<endl;

    cin >> N;

    

    cout << "Enter Range" <<endl;

    cin >> Range;

    

    cout << "Number of cases" <<endl;

    cin >> NumberOfCases;

    

    vector<double> range_time_Vector(Range);

    

    int K = N;

    

    //get the time at this position –start_time-

    time(&start);

    clock_t t1=clock();

    

    for(int i=0; i < Range; ++i) {

        for (int m=1 ; m <= NumberOfCases; ++m) {           

        }

    }

    

    //get the time at this position –end_time-

    time(&end);

    clock_t t2=clock();

    

    //get the difference in between start and end and assign it to time_sum

    time_sum = double(t2-t1)/CLOCKS_PER_SEC;

    time_average = time_sum/NumberOfCases;

    cout<<"time sum (using clock) : "<<time_sum<<endl;

    cout<<"time average (using clock) : "<<time_average<<endl;

    

    time_sum = (double)difftime(end,start);

    time_average = time_sum/NumberOfCases;

    cout<<"time sum (using difftime) : "<<time_sum<<endl;

    cout<<"time average (using difftime) : "<<time_average<<endl;

    

    

    //get the average and add it to the vector

    

    // I don't know how to assign value to vector variable


    getch();

    return 0;

}


There was no error I found.

I run program using dev-cpp in windows platform

The following attachment is result of program

Attached Files



#12
kmhosny

kmhosny

    Programmer

  • Members
  • PipPipPipPip
  • 133 posts
if you want to print the time averages at the end.
for(int i=0 ; i<range_time_vector.size() ; i++)
  cout<<range_time_vector[i]<<endl;
can you post the last code you wrote to be able to help you ?
"Recursion is just a line of code"
-Karim Hosny-
My flickr