Question

I am using xcode Use the following ideas to develop a nonrecursive, linear-time algorithm for the...

I am using xcode

Use the following ideas to develop a nonrecursive, linear-time algorithm for the maximum-subarray problem. Start at the left end of the array, and progress toward the right, keeping track of the maximum subarray seen so far. Knowing a maximum subarray of A[1..j], extend the answer to find a maximum subarray ending at index j + 1 by using the following observation: a maximum subarray of A[1..j + 1] is either a maximum subarray of A[1..j] or a subarray A[i..j + 1], for some 1 ≤ i ≤ j + 1. Determine a maximum subarray of the form A[i..j + 1] in constant time based on knowing a maximum subarray ending at index j.

Your program is to be written in C++. Please name your file: linear_max_subarray.cpp. You’re program needs to do the following:

Receive the name of an input text file via a command line argument.

Open the given file and read all of the values into a std::vector of longs. An example input file is given below.

Finally, your program should calculate the maximum subarray for the provided data and output the value to stdout (e.g., using cout).

Notes:
- Your program should compile with the following command: g++ -std=c++11 -Wall -o linear_max_subarray linear_max_subarray.cpp.
- Your program should run correctly with the following command: ./linear_max_subarray inputs.txt.
- Your program will be tested against one file of all positive numbers, one file of all negative numers, and two files with randomly generated numbers.
- It is important the only output from your program be the number representing the maximum subarray value. Do not output extra text in the version that you submit.

Your completed program should be uploaded to Gradescope. You should receive fairly immediate feedback.

Example input file contents:

-4
6
3
7
-10
3
4
4
-8
-1


Example showing how to read command-line inputs:

// This is a simple example showing how you can read
// arguments from the command line.
//
// You can run the program as follows:
//  ./ one two three
//
// And you will get the following output:
//  Argument 0 is: ./
//  Argument 1 is: one//  Argument 2 is: two
//  Argument 3 is: three
//
// In your programming assignment, your program should
// read the second input (argv[1]) and treat it as the
// name of the file that it will read.

#include 
using std::cout;
using std::endl;

int main(int argc, char const *argv[])
{
    // An example showing how to read command line arguments
    for (auto i = 0ul; i < argc; ++i) {
        cout << "Argument " << i << " is: " << argv[i] << endl;
    }
    return 0;
}
0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include <iostream>
#include <fstream>
#include <vector>
#include <limits.h>

using namespace std;

int* maxSubArray(vector<long> &v, int size)
{
static int maxindex[2];
int maxSumTillNow = INT_MIN;

int currentStartIndex = 0;
int currentSum = 0;

for (int i = 0; i < size; i++)
{
    currentSum = currentSum + v[i];

   if(currentSum > maxSumTillNow)
   {
    maxSumTillNow = currentSum;
    maxindex[0] = currentStartIndex;
    maxindex[1] = i;
   }

   if(currentSum<0)
   {
    currentSum = 0;
    currentStartIndex = i + 1;
   }
}

return maxindex;
}


/* Driver program to test maxSubArray */
int main(int argc, char const *argv[])
{
   if(argc <2)
      cout << "Invalid input arguments\n";

   int data;
   vector<long> v;

   ifstream myfile;
   myfile.open (argv[1]);

   while(myfile >> data)
   {
      v.push_back(data);


   }

   myfile.close();

   int *result = maxSubArray(v, v.size());

   cout << "Largest sum subarray: ";
   for (int i = result[0]; i <= result[1]; ++i)
   {
      cout << v[i] << " ";
   }

   cout << endl;

   return 0;
}


/*
input.txt

-4
6
3
7
-10
3
4
4
-8
-1

output:

Largest sum subarray: 6 3 7 -10 3 4 4

*/

Add a comment
Know the answer?
Add Answer to:
I am using xcode Use the following ideas to develop a nonrecursive, linear-time algorithm for the...
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
  • Objective: Use input/output files, strings, and command line arguments. Write a program that processes a text...

    Objective: Use input/output files, strings, and command line arguments. Write a program that processes a text file by removing all blank lines (including lines that only contain white spaces), all spaces/tabs before the beginning of the line, and all spaces/tabs at the end of the line. The file must be saved under a different name with all the lines numbered and a single blank line added at the end of the file. For example, if the input file is given...

  • I need only one  C++ function . It's C++ don't write any other language. Hello I need...

    I need only one  C++ function . It's C++ don't write any other language. Hello I need unzip function here is my zip function below. So I need the opposite function unzip. Instructions: The next tools you will build come in a pair, because one (zip) is a file compression tool, and the other (unzip) is a file decompression tool. The type of compression used here is a simple form of compression called run-length encoding (RLE). RLE is quite simple: when...

  • Hi, need this question ansered in c++, has multiple levels will post again if you can...

    Hi, need this question ansered in c++, has multiple levels will post again if you can complete every level so keep an eye out for that. here is a sketch of the program from the screenshot int main (int argc, char** argv) { enum { total, unique } mode = total; for (int c; (c = getopt(argc, argv, "tu")) != -1;) { switch(c) { case 't': mode = total; break; case 'u': mode = unique; break; } } argc -=...

  • I am writing a program in C++, which requires me to read an input text file...

    I am writing a program in C++, which requires me to read an input text file using command line argument. However, I am using xcode on my Macbook to write C++ program, and use terminal instead of command. How do you use int main(int argc, char** argv[]) to read an input file. My professor requires us not to hard code the text file name like .open("example.txt"); Thank you!

  • Program already solved, but I need to put function documentation headers for each and every function...

    Program already solved, but I need to put function documentation headers for each and every function in your program (for the ones you write, and keep the function header I give you for the main() function). Also make sure you keep the file block header at the top of the file, and fill in the information correctly. Secondly this week you must get your indentation correct. All indentation must use 2 spaces, and you should not have embedded tabs in...

  • C++ problem. hi heys, i am trying to remove beginning and the end whitespace in one...

    C++ problem. hi heys, i am trying to remove beginning and the end whitespace in one file, and output the result to another file. But i am confused about how to remove whitespace. i need to make a function to do it. It should use string, anyone can help me ? here is my code. #include <iostream> #include <iomanip> #include <cstdlib> #include <fstream> using namespace std; void IsFileName(string filename); int main(int argc, char const *argv[]) { string inputfileName = argv[1];...

  • 1. Suppose you wrote a program that reads data from cin. You are now required to...

    1. Suppose you wrote a program that reads data from cin. You are now required to reimplement it so that you can read data from a file. You are considering the following changes. I. Declare an ifstream variable in_file II. Replace all occurrences of cin with in_file III. Replace all occurrences of > > and get_line with the appropriate operations for ifstream objects What changes do you need to make? I, II, and III II and III I and III...

  • The program is written in c. How to implement the following code without using printf basically...

    The program is written in c. How to implement the following code without using printf basically without stdio library? You are NOT allowed to use any functions available in <stdio.h> . This means you cannot use printf() to produce output. (For example: to print output in the terminal, you will need to write to standard output directly, using appropriate file system calls.) 1. Opens a file named logfle.txt in the current working directory. 2. Outputs (to standard output usually the...

  • I keep getting an error code in my C++ program. It says "[Error] 'strlen' was not...

    I keep getting an error code in my C++ program. It says "[Error] 'strlen' was not declared in this scope" in my main.cpp. Here are my codes. main.cpp #include <iostream> #include <string> #include "functions.h" using namespace std; //definition of the main function. //takes arguments from the command-line. int main(int argc, char *argv[]) { //Determine if you have enough arguments. //If not, output a usage message and exit program if (argc<2 || (argc == 2 && argv[1][0] == '-')) { //call...

  • C Language Programming. Using the program below - When executing on the command line only this...

    C Language Programming. Using the program below - When executing on the command line only this program name, the program will accept keyboard input and display such until the user does control+break to exit the program. The new code should within only this case situation “if (argc == 1){ /* no args; copy standard input */” Replace line #20 “filecopy(stdin, stdout);” with new code. Open read a text file “7NoInputFileResponse.txt” that contains a message “There were no arguments on the...

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
ADVERTISEMENT