C/C++ :: Running Array In Less Complexity

Feb 9, 2015

Giving a dynamic array, we want to pass the array elements from a file. The first number in the file N gives us the array length. They follow N numbers, the actual elements of the array.

Three brothers are going to work in the shop of their father for a time. The shop is doing well and every day generates profit which the three brothers may provide. The brothers agreed that they would divide the total time in three successive parts, not necessarily equal duration, and that each one will be working in the shop during one of these parts and collects the corresponding profit on his behalf. But they want to make a deal fairly, so that one received from three more profit someone else. Specifically, they want to minimize the profit the most favored of the three will receive.

I first created a program that WORKS! with complexity O(n3).

#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
int sum_array(int* array, int cnt){
int res = 0;
int i;
for ( i = 0; i < cnt ; ++i)

[Code] .....

Let's assume that we have the following input:

10

1 1 8 1 1 3 4 9 5 2

The ouptut should be 15 -> A = 1 + 1 + 8 + 1 + 1 + 3 = 15 , B = 4 + 9 = 13 , C = 5 + 2 = 7.

But the ouptut is 16!

How can I reduce the complexity of my first working! C code?

View 9 Replies


ADVERTISEMENT

C :: Filling 2D Array Complexity?

Mar 24, 2013

Assuming that we have :

Code:
int arr2d[rows][columns] ; // Not valid syntax of course ... let be arr2d rows * columns size
for(int i=0; i<rows; i++)
for(int j=0; j<columns; j++)
arr2d[rows][columns] = some_value;

What is the complexity? I believe O(n) and not O(n^2) on this case because if you have 3*3 size you would put 9 elements (from 9 elements input of course)... for each input you have one insertion and that is the meaning. Same as 4*4 size 16 input times 16 insertions .. or 5*5 and so forth...

View 8 Replies View Related

C++ :: Dynamically Resizing Array While Program Is Running?

Feb 4, 2015

I'm working the 4th problem in chapter 14 of the Jumping into C++ book. In the book, he gives an example program for dynamically resizing an array while the program is running. It works fine for integer types but i need to do the same with a string type array. Right now my program is crashing because the string array is not resizing itself. Here's the part of the code im trying to figure out. The part for the int array has been ignored using // since it works fine and I'm trying to figure out whats wrong with the string array.

Code:
#include <iostream>
#include <string>
//Write a program that lets users keep track of the last time they talked to each of their friends.
//Users should be able to add new friends (as many as they want!) and store the number of days ago

[Code]......

View 8 Replies View Related

C++ :: Space Complexity Of A Map

Feb 1, 2013

What would the worst, average and best case space complexity be for a data structure of type map<string, vector<int> > in big O notation? I'm parsing through a document and storing each word as a key and im attaching an associated int (in a vector) to it as the value.

View 4 Replies View Related

C/C++ :: Changing Complexity From O(n) To O(1)

May 5, 2012

For the following code :

 s = 0 ;
 for(i=m ; i<=(2*n-1) ; i+=m) {
            if(i<=n+1)
            { s+=(i-1)/2 ; }
           else
            { s+=(2*n-i+1)/2 ; }  
      }  

I want to change the complexity of the code from O(n) to O(1) . So I wanted to eliminate the for loop . But as the sum "s" stores values like (i-1)/2 or (2*n-i+1)/2 so eliminating the loop involves tedious calculation of floor value of each (i-1)/2 or (2*n-i+1)/2 . It became very difficult for me to do so as I might have derived the wrong formula in sums of floors . Need Changing complexity from O(n) to O(1). Is there any other way to reduce the complexity ? If yes ... then how ?

View 3 Replies View Related

C++ :: Reducing The Time Complexity?

Jul 21, 2014

I have a very simple program the time complexity of the function that I used in this program is O(mn)because it has a nested loop, I really need to reduce the time complexity to O(n)

[code=c++]
#include <iostream.h>
#include<stdlib.h>
int *char_count( const char* DNA, const int *starts, const int *ends, char letter);
int main()

[Code].....

View 5 Replies View Related

C :: Space Complexity Of Recursive Function

Jan 23, 2013

this function has to check if an array is not descending it has to be recursive and it's space complexity has to be O(log(n))

Code:

int is_sorted(int a[ ], int n) {
int check=n-1;
if(n==1 || n==0) return 1;
if(a[check]<a[check-1])return 0;
else return(is_sorted(a,n-1));
}

this is what i came up with. i know it's space complexity is O(n) how do i make it O(log(n))?

View 2 Replies View Related

C :: Space Complexity Of Radix Sort?

Sep 2, 2013

I was just going through Radix Sort algorithm. Though I got the logic and the concept used in it, I am still pretty much confused about the space complexity being O(k+n) for this algorithm. (where, k is the max no of digits in a number, and n is the no. of inputs to be sorted).

View 1 Replies View Related

C++ :: Algorithm Has Asymptotic Complexity Recursive?

May 22, 2013

would like to know that my algorithm has asymptotic complexity recursive?

int solve(pair<int,int> actual)
{
if(actual.first == PuntoInicio.first && actual.second == PuntoInicio.second)
return 1;

[Code]....

View 1 Replies View Related

C/C++ :: Algorithmic Complexity Of Switch Statement

Feb 14, 2014

Suppose you have a switch statement defined like:

switch (parameter) {
case ONE:
case TWO:
// ... other n-2 cases
case N:
doSomething();
break;
default:
somethingElse();
break;
}

What is the complexity of the doSomething()? Is it still equal to the complexity of doSomething() , or the complexity of doSomething() plus the number of case statements? In other words, does the number of case statements a conditional piece of code has count towards the complexity?

View 10 Replies View Related

C++ :: How To Calculate Time And Space Complexity Of Algorithm

Jan 25, 2015

define the time and space complexity of an algorithm and about what can i do when the time and space complexity are given and i should write the code corresponding to the restrictions , which i find very difficult.

View 1 Replies View Related

C/C++ :: Finding Average Time Complexity Of A Nested Loop?

Oct 29, 2014

We were discussing how to find average time complexity of different algorithms. Suppose I've a the following nested loop

for(int i=0;i<n;i++)
{
min = i;

[Code].....

Now the outer loop will iterate N times. the inner loop will always iterate 3 times no matter what the value of N is. so the worst case time complexity should be O(n^2) but what about the average case and the best case? I mean in terms of searching we can figure out and distinguish between the worst,best and average as it depends upon the number of comparisons. But here how can the average and best case be different then the worst case.

View 13 Replies View Related

C++ :: Running A Program Outside IDE

Jul 23, 2014

after you have compiled a program can you run it without an IDE?

View 17 Replies View Related

C++ :: Running Bat Files?

Oct 24, 2013

I want to know if you can run bat files from code? If so how else how can you shutdown a computer with code? OS is windows 7.

View 2 Replies View Related

C++ :: COM Exe Not Running On A Particular Machine?

Jul 23, 2012

I have a COM exe which runs fine on all machines that I have used except one. On a particular machine, the COM exe does not start even after trying to execute it manually (do a double click on the file from explorer).

There are no error messages as well. what could be happening ?

View 8 Replies View Related

C++ :: How To Prevent Loop Running Over

Feb 18, 2014

i am writing a function that takes a delimited string and splits the strings into it's respective words according to the delimeter.

1) iterate through string and store delimeter position in vector array.

2) iterate through again and use substr to split into words and store again in a vector.

I then have a dictionary file, and am comapring each values in the string with each in the dictionary, problem is that it overruns the loop and obviously gives a subscript out of range error.

Code:
#include <iostream>#include <fstream>
#include <vector>
using namespace std;
//Start with string inputString
//Find delimeters ::pos as ints and stores positions in vector <int> array
//Run though string using 'find' and seperate string by positions of char32s
//Build vector<string> array of individual words in string
//Open dictionary file and loop though testing each words in vector string array against each word in dictionary

[code]....

View 4 Replies View Related

C++ :: Loop Is Running Infinitely

Aug 7, 2014

My program is supposed to end when both a and b are 1. Where it is wrong.

Code:
#include<iostream>
#include<cstdlib>
#include<ctime>
using namespace std;
int roll()

[Code] ....

View 2 Replies View Related

C++ :: Checking If A Program Is Running?

Jun 17, 2014

I have a question, how can I check if a program is running using c++? For example

if (notepad.exe is running) {
.... ..... ....

View 3 Replies View Related

C/C++ :: How To Determine If Application Is Already Running On Mac OSX

Dec 18, 2014

I have a service A, and an application B. Service A needs to launch application B only if it's not running currently. This can be easily done in Windows by calling GetExitCodeProcess function. I cannot find an equivalent method for doing so in Linux/Mac.

So my current code says:

system("open /Users/adsmaster/client/client &"); // to launch the application from the service in a new shell

I read that on a Linux-line machine you can use $ cal to get the exit value of the recently run process but I am not sure how can get the exit value of a particular process?

View 1 Replies View Related

C++ :: Monitor Cpu Usage While App Is Running

Jul 15, 2012

I am working on a parallel processing server/client app with a multi-threaded server and several data processing clients, each their own process. I am trying to optimize some of the parameters, such as the size of the chunks that are read, processed, and output, and also some of the timeout values and such. I can track the time to finish a given task well enough, but it would be really nice to be able to track the cpu use. When CPU use is near 100% (on all cores) for the entire run, that is a good sign. I have noticed that with some combinations of parameters, CPU drops quite a bit for long stretches, which is not such a good sign. This app process large input files (2.5GB-65GB so far) and needs to be stable for long periods of time.

Other than sitting and staring at the task manager all day, is there a good way to track/log the CPU usage over runs that take many hours? I know that there are some apps like Everest that chart CPU use, but it would be nice to have something that would write to the same log file I am already using for other program output.

View 1 Replies View Related

C++ :: Running A SLR Parser Program?

Apr 27, 2012

I need to run a program that makes a SLR Parser Table.

Here is the code :

Code to find first and follow:

saved as SLR.h
#include<stdio.h>
#include<ctype.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>
#include<iostream.h>
#define epsilon '^'

since I didn't know how to type epsilon symbol temporarily I am using ^

char prod[20][20],T[20],NT[20],c[10][10],foll[10][10],fir[10][10];
int tt,tnt,tp,a;
int follow[20][20],first[20][20];
void first_of(char);
int count(int j);
void rhs(int j);

[code]....

View 3 Replies View Related

C :: Running Linear Actuator Without Feedback

Jan 4, 2014

I am trying to do single axis solar tracker with linear actuator /rtc/ UNO. I have already done with feedback sensor.

Now Here I am trying to without feedback. Linear actuator specification: 24v , 3.2mm/sec as speed , 600mm stoke.

Desired angle calculation:
tracking start from 7am to 18PM, 11hours
Assumed degree: 7AM as -45 deg , 12.30 as 0 degree and 18 pm as 45 degree.
static float slope= 0.00227272727273;
static float intercept=- 102.272727273;

[Code] ....

How can i put time here. Coding for calculating Ton time and solve above equation. below i posted my code . I need it has to be modified little bit. i need to implement ton time actuator here.

I need function takes desired and actual angle , where actuator try to move to its actual desired position.

Code below in arduino version

Code:
double Desire_Degree;
unsigned int TS;
static float slope= 0.00227272727273;
static float intercept=- 102.272727273;
static int length;
double Actual_Degree;

[Code] .....

View 1 Replies View Related

C :: Variable Is Unexpectedly Set Zero While Running While Loop

Sep 4, 2013

I ran into a problem while using while loop.T he declared and initiated local int variable works well with its specified value while running through the 1st run of a while loop. It is set zero while entering the 2nd run and the following unexpectedly. However, the variable still exits. The following is the code with problem.

Code:

#include <stdio.h>
int main(void){
const int x=6;
char c='y';

[Code]....

View 10 Replies View Related

C :: Queue Program Not Running After Pressing 1

Apr 20, 2013

Code:

#include<stdio.h>
#include<process.h>
struct que

[Code].....

View 1 Replies View Related

C :: Running A VBS Script Int Without Command Window

Dec 19, 2014

I have created an scientific calculator application and I have a VBS script that runs some of the files. I want to create a C program that runs that VBS script. however, a command window flashes before the application opens. So basically I need a short C (or C++) program that runs a VBS script without a command window popping up. I am on Windows 8 and my compiler is GCC/G++ under cygwin. Here is what I have now that DOES NOT work.

Code:

#include <windows.h>
int main()
{
ShellExecute( NULL, NULL, "StartUp.vbs", NULL, NULL, SW_HIDE);
return 0;
}

View 2 Replies View Related

C++ ::  Reading Running Process From OS And Displaying It

Dec 21, 2013

This code will read the running process from OS and display it (C++). Specifically, the OS here is Windows XP. The Problem(error) is in (i think) prototype. By the way, it displays following errors.

Error 1 : error LNK2019: unresolved external symbol _EnumProcesses@12 referenced in function _main
Error 2 : error LNK2019: unresolved external symbol _GetModuleBaseNameA@16 referenced in function "void __cdecl DisplayProcessNameAndID(unsigned long)" (?DisplayProcessNameAndID@@YAXK@Z)
Error 3 : error LNK2019: unresolved external symbol _EnumProcessModules@16 referenced in function "void __cdecl DisplayProcessNameAndID(unsigned long)" (?DisplayProcessNameAndID@@YAXK@Z)
Error 4 : fatal error LNK1120: 3 unresolved externals C:Documents and SettingsWindowsMy DocumentsVisual Studio 2008ProjectsaDebuga.exe

#include <afxwin.h>
#include <iostream>
#include <string.h>
#include "psapi.h"
unsigned int i;
using namespace std;

[Code] .....

View 2 Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved