Question

Below is my c++ code. For the first section how do I include multiple text files...

Below is my c++ code. For the first section how do I include multiple text files such as file8, file 25, file 50, file 125? Also, for algorithm 1 my clock function does not work. I need it to start the clock before the algorithm then run through it and record the end time. Then find the difference between end time and start time and convert it to milliseconds. The print out the max sum and the run time!!!! Im so lost please help/explain this stuff.

#include <iostream>

#include <cstdlib>

#include <cstring>

#include <fstream>

#include <vector>

#include <time.h>

#include <stdio.h>

using namespace std;

int main() {

// store the filename

char filename[100];

// open the file with test8

ifstream file8;

// I think this will prompt lines 0-100 from the file I said to open

cout << "file8.txt:";

cin.getline(filename, 100);

// actually open the file

file8.open(filename);

// put the numbers from the file into a vector

vector <int> numbers;

// this if fuction will read through all the numbers in the file and make the size of the vector as large as the file

if(file8)

{

int number;

while (file8 >> number)

numbers.push_back(number);

}

for (int i=0; i < numbers.size(); i++)

{

cout<< numbers.at(i) << ' ';

}

};

/**

Algo 1

*/

int maxSubSum1( const vector <int> & a )

{

double run_time = 0.00;

clock_t begin = clock();

int maxSum = 0;

  

for( int i = 0; i < a.size( ); ++i )

for( int j = i; j < a.size( ); ++j )

{

int thisSum = 0;

  

for( int k = i; k <= j; ++k )

thisSum += a[ k ];

  

if( thisSum > maxSum )

maxSum = thisSum;

}

clock_t end = clock();

return maxSum;

run_time += (double)(end - begin) / CLOCKS_PER_SEC;

printf("Run time of algorithm is %f seconds", run_time);

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include <iostream>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <vector>
#include <time.h>

using namespace std;

int maxSubSum1( const vector <int> & a );

int main() {

        // add your more files here and change number of files.
        int numFiles = 3;
        cout << "how many files? ";
        cin >> numFiles;
        cin.ignore();

        string fName;

        for(int i=0; i<numFiles; i++) {
                cout << "Enter file name: ";
                getline(cin, fName);

                ifstream file(fName.c_str());

                vector <int> numbers;
                int number;
                while (file >> number)
                        numbers.push_back(number);

                for (int i=0; i < numbers.size(); i++) {
                        cout<< numbers.at(i) << ' ';
                }
                cout << endl;
                int result = maxSubSum1(numbers);

                cout << endl << "Max sum: " << result << endl << endl << endl;
        }

};

/**
Algo 1
*/
int maxSubSum1( const vector <int> & a ) {

        double run_time = 0.00;
        clock_t begin = clock();
        int maxSum = 0;

        for( int i = 0; i < a.size( ); ++i ) {
                for( int j = i; j < a.size( ); ++j ) {

                        int thisSum = 0;

                        for( int k = i; k <= j; ++k )
                                thisSum += a[ k ];

                        if( thisSum > maxSum )
                                maxSum = thisSum;
                }
        }

        clock_t end = clock();

        run_time += (double)(end - begin) / (CLOCKS_PER_SEC / 1000);

        printf("Run time of algorithm is %f milliseconds", run_time);
        
        return maxSum;
}

please upvote. Thanks!

Add a comment
Know the answer?
Add Answer to:
Below is my c++ code. For the first section how do I include multiple text files...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Merge Sort: Time Complexity: O(n log(n)) #include "stdafx.h" #include <iostream> #include <time.h> #include <stdlib.h> using namespace...

    Merge Sort: Time Complexity: O(n log(n)) #include "stdafx.h" #include <iostream> #include <time.h> #include <stdlib.h> using namespace std; void combine(int *a, int low, int high, int mid) {        int i, j, k, c[100000];        i = low;        k = low;        j = mid + 1;        while (i <= mid && j <= high)        {               if (a[i] < a[j])               {                      c[k] = a[i];                      k++;                      i++;               }               else               {                     ...

  • How would I be able to get a Merge Sort to run in this code? MY...

    How would I be able to get a Merge Sort to run in this code? MY CODE: #include <iostream> #include <fstream> #include <stdlib.h> #include <stdio.h> #include <time.h> using namespace std; class mergersorter { private:    float array[1000] ;    int n = 1000;    int i=0; public:    void fillArray()    {        for(i=1;i<=n;i++)        {            array[i-1]= ( rand() % ( 1000) )+1;        }    }    void arrayout ()    {   ...

  • C Programming // Compile with: clang radio.c -o radio // Run with: ./radio #include <stdio.h> #include...

    C Programming // Compile with: clang radio.c -o radio // Run with: ./radio #include <stdio.h> #include <stdlib.h> #include <string.h> #include <time.h> // @Name    loadMusicFile // @Brief   Load the music database //          'size' is the size of the database. char** loadMusicFile(const char* fileName, int size){ FILE *myFile = fopen(fileName,"r"); // Allocate memory for each character-string pointer char** database = malloc(sizeof(char*)*size); unsigned int song=0; for(song =0; song < size; song++){       // Allocate memory for each individual character string       database[song] =...

  • Need help adjusting this code in C. I'm not sure this one part of the code...

    Need help adjusting this code in C. I'm not sure this one part of the code is correct, it needs to generate n random addresses between 0 and (2^32)-1. So I set it to "address = rand() % 232;" but I'm not sure if that is correct. Can you please fix thanks. #include <stdio.h> #include <stdlib.h> #include <time.h> int main() { unsigned int address; unsigned int pg_num; unsigned int offset; unsigned int i; unsigned int n = 1000000; double time_taken;...

  • graph binary search for size and time c++ //System Libraries #include <iostream> #include <string> #include <cstdlib> #include <ctime> #include <iomanip> #include <alg...

    graph binary search for size and time c++ //System Libraries #include <iostream> #include <string> #include <cstdlib> #include <ctime> #include <iomanip> #include <algorithm> using namespace std; //User Libraries //Global Constants, no Global Variables are allowed //Math/Physics/Conversions/Higher Dimensions - i.e. PI, e, etc... //Function Prototypes //Execution Begins Here! int main(int argc, char** argv) { int n, i, arr[50], search, first, last, middle,count=0,count_in,tot; clock_t start, end; float duration; cout<<"Enter total number of elements :"; cin>>n; cout<<"Enter numbers"; for (i=0; i<n;i++) cin>>arr[i]; cout<<"Enter a...

  • Hello, I want to check if my C++ code is correct and follows the requeriments described...

    Hello, I want to check if my C++ code is correct and follows the requeriments described thanks. Requeriments: Assignment Sorting Benchmark each of the sorting methods listed below. Insertion Sort Bubble Sort Selection Sort Heap Sort. Quick Sort. Merge Sort. Benchmark each of the above sorting methods for data sizes of 10000, 20000, 30000, 40000 and 50000. Display the results in a table as shown below. The table should have rows and columns. However, the rows and columns need not...

  • In c++ please How do I get my printVector function to actually print the vector out?...

    In c++ please How do I get my printVector function to actually print the vector out? I was able to fill the vector with the text file of names, but I can't get it to print it out. Please help #include <iostream> #include <string> #include <fstream> #include <algorithm> #include <vector> using namespace std; void openifile(string filename, ifstream &ifile){    ifile.open(filename.c_str(), ios::in);    if (!ifile){ cerr<<"Error opening input file " << filename << "... Exiting Program."<<endl; exit(1); }    } void...

  • C++ how can I fix these errors this is my code main.cpp #include "SpecialArray.h" #include <...

    C++ how can I fix these errors this is my code main.cpp #include "SpecialArray.h" #include <iostream> #include <fstream> #include <string> using namespace std; int measureElementsPerLine(ifstream& inFile) {    // Add your code here.    string line;    getline(inFile, line);    int sp = 0;    for (int i = 0; i < line.size(); i++)    {        if (line[i] == ' ')            sp++;    }    sp++;    return sp; } int measureLines(ifstream& inFile) {    // Add your code here.    string line;    int n = 0;    while (!inFile.eof())    {        getline(inFile,...

  • How do can I update this code (Code A): Code (A) #include using namespace std; int fibonacci(int n) { int a = 0,...

    How do can I update this code (Code A): Code (A) #include using namespace std; int fibonacci(int n) { int a = 0, b = 1, c; if (n <= 1) return n; for (int i = 2; i <= n; i++) { c = a + b; a = b; b = c; } return b; } int fibonacciRecursive(int n) { if (n <= 1) { return n; } return fibonacciRecursive(n-1) + fibonacciRecursive(n-2); } int main() { int n;...

  • In this lab we are going to complete a profile of two sorting algorithms by running...

    In this lab we are going to complete a profile of two sorting algorithms by running some tests to collect empirical data. 1. First we need to be able to generate some random integers. You can do this by including the following library : #include Now first run the following to generate a seed : srand (time(NULL)) You can then generate a random number using the function rand() 2. We will use two sort algorithms - Selection Sort and Bubble...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT