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;
}
#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
*/
I am using xcode Use the following ideas to develop a nonrecursive, linear-time algorithm for the...
Command line input In C++ it is possible to accept command line arguments. Command-line arguments are given after the name of a program in command-line operating systems like Linux and are passed in to the program from the operating system. To use command line arguments in the program, it must first understand the full declaration of the main function, which until now has accepted no arguments. In fact, main can accept two arguments: one argument is number of command line...
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 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...
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 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 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 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 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 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...
can you please split this program into .h and .cpp file #include <iostream> #include<string> #include<fstream> #define SIZE 100 using namespace std; //declare struct struct word_block { std::string word; int count; }; int getIndex(word_block arr[], int n, string s); int main(int argc, char **argv) { string filename="input.txt"; //declare array of struct word_block word_block arr[SIZE]; int count = 0; if (argc < 2) { cout << "Usage: " << argv[0] << "...