C++ :: Code That Returns Maximum Sum Of A Subsequence From A Array
Aug 25, 2014
Here's the code that returns maximum sum of a subsequence from a given array. This is according to "Programming Pearls" by Jon Bentley. I have come up with an example for which this program won't work. And here's that example:
{ -10, 11, 10, -10, 2, 3, -6, 1 }
Max subsequence sum above is 21. 2nd and 3rd elements.
But the program returns 16. It sums 2nd through 6th elements and returns. Why would the writer explain something with such depth, only to give a program that doesn't work in all instances?
int MaxSum(int lo, int hi, int* arr) {
if (lo > hi) {
return 0;
}
if (lo == hi)
[Code]...
View 11 Replies
ADVERTISEMENT
Mar 12, 2014
I got a code written in Java. But, I gave up writing code in Java. The program written is supposed to find the maximum subsequence sum. It's originally like this.
Code:
private static int maxSumRec (int [] a, int left, int right)
{
if(left == right)
if(a[left > 0])
return arr[left];
[Code] .....
I turned it into C, add some elements (to generate random numbers and change some variables' names), and becomes like this
Code:
int maxSumRec (val, left, right)
{
int x;
long int arr[val];
srand ( time(NULL) );
for(x=0; x<val; x++)
[Code] .....
It fails to compile. What have I done wrong? And I keep wondering why in the original code there is left and right variables and their values are never assigned. My c compiler (I use codeblocks) keeps telling me that. Idk why. My friend who keeps it in Java says it is fine but he cannot explain how his program works. What *is* left and right actually?
View 8 Replies
View Related
Jan 21, 2013
Write a function that takes an array and returns true if all the elements in the array are positive, otherwise, it returns false.
View 6 Replies
View Related
Dec 24, 2013
to return the array i shall make a pointer function thats ok.. but how do I get the size return if i dont know the size?
if I need to make AXB=C and output C my new array doesnt have a size..
View 7 Replies
View Related
Oct 29, 2014
Any way to determine the highest value of an array I created with random numbers. I am confused because the array needs to be initialized in the main, but populated in a function. I was able to populate it using a pointer variable and my results came out good for the initial array values and elements.
In order to figure out the max, I think I would need the results of the populated array. How do I do this when the populated array is stored in a pointer variable? Would I need to create a pointer to the first pointer I created? I tried creating another pointer to the initial array and printing that, but my results were not good.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
[Code].....
View 10 Replies
View Related
Mar 1, 2013
I do not understand how I can implement this.If fread != to at least 8 bytes then do THIS: printf (" your file is near the end of file", fread result);
View 8 Replies
View Related
May 1, 2013
So I need to write a function that has 2 arguments that are strings. It returns 1 if the first string is a subsequence of the second string, and 0 otherwise.
For example, given 2 strings like "foobar" and "obr", the function should returns 1 whereas "foobar" and "obo" returns 0.I have tried writing the code myself,
Code:
int sub(char *s, char *t) {
int i;
char *p;
for (i=0; t[i] != ''; i++) {
p=strchr(s,t[i]);
if (p==NULL)
return 0;
else
strcpy(s,p);
}
return 1;
}
So my strategy is as follows:
1. Find the first letter of the second string in the first string (foobar).
2. If there's a match then continue to search for the second letter of the second string starting from the index of the first letter plus 1. (obar). Otherwise return 0,.........
However the function always return 1 no matter what the input strings are.
View 11 Replies
View Related
Sep 11, 2014
In the implementation of a program to find the length of the longest common subsequence, what does line 14 do?
void lcs( char *X, char *Y, int m, int n )
{
int L[m+1][n+1];
/* Following steps build L[m+1][n+1] in bottom up fashion. Note
that L[i][j] contains length of LCS of X[0..i-1] and Y[0..j-1] */
for (int i=0; i<=m; i++)
[Code].....
View 2 Replies
View Related
Aug 2, 2014
I was given some practice problems in my programming class, to prepare for the final and I don't quite understand what this one is asking for exactly:
Write the remainder of the program to find the maximum value for the middle row of the array data. Print the max after finding it. Your code should work for any 2D array of ints with three rows and four columns, so don't hard-code your program to these specific values.
Code:
#include <stdio.h>
int main(void)
{
int data[][4] = {{ 5, 8, 7, 2},
{ 3, 10, 1, 9},
{ 6, 12, 5, 0}};
View 8 Replies
View Related
Feb 3, 2014
How will you code a program that displays the maximum value in a two dimensional array of integers. the program will ask the user to input the 4x5 array?
i know how to code it. but i dont know what to do to find the maximum value. :( how to find the maximum?
View 4 Replies
View Related
May 22, 2014
i have tried like that int arr[1000000] to initialize but it crashed my programm.but if i do int arr[100000] it works fine..why is that and what is the maximum range of integer array initialization??
View 5 Replies
View Related
Mar 20, 2013
I have an assignment which requires me to do the following:
Required to write a function that finds an integer in an array and returns its corresponding index. The function must be called findNumber. It must have FOUR parameters:
- The first parameter is the array to be searched
- The second parameter is the integer to be found within the array
- The third parameter is the size of the array
- The fourth parameter is an integer that indicates whether the array is sorted. A value of 1 means the array is sorted; a value of zero means the array is not sorted.
Since a function can only return one value(To return the position of a required integer in an array in this instance) I have tried to make use of pointers to try and return a value stating whether the array is sorted or not.This is my code : (It compiles perfectly but it does not produce any outputs)
Code:
#include <stdio.h>
#define SIZE 10
size_t findNumber(int *sort, const int array[],int key,size_t size);
int main(void){
int a[SIZE];
size_t x;
[code].....
View 1 Replies
View Related
Sep 12, 2014
I found this implementation on a website for printing the longest common subsequence. But it gives wrong answers for some reason even though the code seems right to me.
Here is the code:
#include <iostream>
int lcs(char *X, char *Y, int m, int n)
{
int L[m+1][n+1];
for(int i = 0; i<=m; i++)
[code].....
What is wrong with this code?
View 7 Replies
View Related
Oct 9, 2014
I'm trying to get correct answers from my exam to study for a midterm:
Write a function that returns the maximum value of an array of doubles.
Declaration:
double maximum(array[],int a_size);
So far I've done:
double maximum(array[], int a_size) {
double maxVal;
maxVal = array[0];
for(int i=1; i < size; i++) {
if(array[i] > max
maxVal=array[i]
}
return maxVal;
}
I just need any corrections.
Second, I need to write a function definition that merges two vectors containing integers, alternating elements from both vectors.
Prototype:
vector<int> merge(const vector<int> a, const vector<int> b);
This is my definition so far:
vector<int>merge(const vector<int> a, const vector<int> b){
vector<int> merged;
for(int i=0; i<a.size+b.size; i++) {
if(flag == true)
merged.push_back(a);
flag = false;
else
merged.push_back(b)
flag = true;}
I think I'm lost in the second part...
View 2 Replies
View Related
Apr 15, 2013
I have assignment which requires me to print out and array code and a pseudo code. I dont no what a pseudo code is,.
View 2 Replies
View Related
Dec 23, 2014
I've made a code to check whether or not a save file has been created correctly, but for some reason it always returns this line: readdata[qa]=='1' as true. in which qa is the counter I use in a for loop and readdata is a character array consisting of 50 characters that are either 0, 1 or 2.
this is the entire code:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
[Code]....
at first is also went wrong at line 22 and also returned that as true, but then I added brackets and it worked.
View 4 Replies
View Related
Apr 11, 2013
So the premise of my code is to read in a 2d array from a .txt. The array is a game board but the first two lines are what determine the size of the board. After i read it in i want it to find wherever te character "U" is, and then display the array but only showing U and whats around it. Problem is i cant get the array to print the right size and the code for displaying the U is not working either.
ifstream inputFile;
int boardSizeRow;
int boardSizeCol;
inputFile.open("C:UsersMichaelDesktopfileboard2.txt");
inputFile >> boardSizeRow;
inputFile >> boardSizeCol;
inputFile.get();
[Code]...
FILE:
20
20
WWWWWWWWWWWWWWWWWWWW
W GO W W
W WW w S W
W H W GW w W
WPW WW G W
WK W W
W W W W w w W
WK WU W
SW w w W
W W
w W G W
G W w W
D wwwww W
K w D W
w w W w w W
ww w WWWWWWW
G w W
ww w S w W
WWW G W
WWWWWWWWWWWWWWWWWWWW
View 5 Replies
View Related
Mar 2, 2015
I made a program that adds two matrices and displays their sum with a max dimension of 100.
/* This program asks the user for 2 matrices called A and B, as integers,
and displays their sum, C. The max dimension of each matrix is 100. */
#include <stdio.h>
// Construct function
void construct() {
int m, n, i, j; // Variables
int first[100][100], second[100][100], sum[100][100]; // Matrices variables
[Code] ....
Now I need to change it so there is no max size for each matrix. The arrays will be larger than 100x100 so I need to use malloc to create my arrays. So I cant use int A[rows][cols]. This is what I did to covert arrays to malloc. It compiles but it crashes after I entered all the integers.
/* This program asks the user for 2 matrices called A and B, as integers,
and displays their sum, C. The max dimension of each matrix is 100. */
#include <stdio.h>
#include <stdlib.h>
// Construct function
void construct() {
int m, n, i, j; // Variables
int *first = NULL;
[Code] .....
View 8 Replies
View Related
Aug 27, 2014
#include <stdio.h>
float total, avg, max, min;
float judge[4];
int index;
int array[4];
int main() {
total = 0.0;
max = array[0];
min = array[0];
[Code] ....
I dont understand how to make the array when it prints out only print out the final average and the final maximum score with the final minimum score but what its doing at the moment is just giving an average for each individual score taken...
Minimum and maximum scores are displaying 0.0
And it displays these things 4 times in a row i just want it to be displayed once.
View 1 Replies
View Related
Feb 7, 2013
I am trying to get each step down before proceeding to the next one for my problem. I have started my program into returning the Area and Volume of a cylinder. The return numbers I get are huge and I cannot figure out why.My header file
class cylinderType {
public:
void getRadius(double rad);
void getHeight(double heigh);
double area();
double volume();
[code]....
View 17 Replies
View Related
Mar 16, 2013
Is it any different when using a class in my code. My previous code i define my struct like this
Code: #include <iostream>
#include <fstream>
#include <string>
struct{
[Code] .....
or do i still define it the same way at the top of my code.
View 4 Replies
View Related
Sep 4, 2013
I have a function that returns a char*. No problem. But I need to concatenate another array with the results of this function. I'm getting a segmentation error.
Code:
//this next line outputs correctly so I know my function is working
fprintf(stdout, "%s
", get_filename(selection));
char* temp;
[Code].....
View 11 Replies
View Related
Nov 27, 2014
Write a C++ program consisting of main plus two other functions which will do the following:
Take an integer input from the keyboard.
Send the integer to a function which will output the integer to the screen.
Send the integer to a second function which will tell the user that the integer is an odd value.
Do not tell the user anything if the integer is an even value.
Repeat this process until the user enters something which is not an integer; use input validation to check for validity.
Any not valid input should terminate the program.
View 3 Replies
View Related
Jun 24, 2013
For a Homework i need to make some kind of Minesweeper but i have one Probleme one of my functions always return teh same and i cannot fin out why here is the code:
bool setzen(int x, int y){
if(feld[x][y]!=3){
return mine=false;
}
[Code]....
Thats the Part where Everything happens but the Function "bool setzen" always returns false and i dont know why.
View 6 Replies
View Related
Feb 1, 2015
I am using Excel 2013, Visual Studio 2015. I began learning about Excel XLL. I wrote simple function which tests whether the argument is missing. Here's code:
#define DllExport __declspec(dllexport)
static LPWSTR rgFuncs[1][7] = { { L"GetSum", L"BU", L"GetSum" } };
DllExport double WINAPI GetSum(LPXLOPER12 arg)
{
if (arg->xltype == xltypeMissing) return -1;
return arg->xltype;
}
This code works as expected: if I miss argument, it gives me -1, and the type otherwise. But when I change code to this:
DllExport double WINAPI GetSum(LPXLOPER12 arg)
{
return (arg->xltype == xltypeMissing) ? -1 : arg->xltype;
}
then, when I miss argument, it gives me 4294967295. Why?
View 2 Replies
View Related
Feb 27, 2014
So, I have been working my way through lines of code, and practicing quite a bit. In fact, I have done programs far more complicated than this and still I cannot get it to work.
The idea of the code is to take a number input by the user through the terminal and check if it is a prime number(It does this) If the number is not prime it takes that number and checks for N amount of primes that is put in through the console. When a prime is found it writes that to the console until the N amount of primes have been found.
#include <iostream>
using namespace std;
int prime(unsigned long long x);
int main() {
long long x;
long long y;
long long z;
[Code] ....
At least that's my goal. Currently it will accept the number and check if it is prime. It will then ask for the N amount of primes but will not return any primes. I have check with numbers a I know have prime factors. I have added the comment //XXXXXXXX To show where the problem appears to occur.
View 1 Replies
View Related