C++ :: Divide And Conquer Algorithm
Feb 6, 2013
I've been stuck on a divide a conquer algorithm problem for about an hour now, and I'm not sure how to solve it. I need to use divide-and-conquer to implement an algorithm that finds the dominant element of an array of positive integers and returns -1 if the array does not have a dominant element (a dominant element is one that occurs in more than half the elements of a given array).
No sorting may be used, and the only comparison that may be used is a test for equality.
I understand the general process I need to follow to solve this problem, but I'm not sure exactly how to convert my thoughts to code. I know that if a number x is the dominant element of an array A, the x must be the dominant element in either the first half of A, the second half of A, or both.
Here is what I have so far.
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int find_dominant(int *A, int p, int r) {
[Code] ....
The program must run in O(n log n) time.
View 2 Replies
ADVERTISEMENT
Mar 6, 2015
In this code nothing modify except function minMaxSearchRecursive
Code:
int min(int a, int b) {
if (a < b) {
return a;
} else {
return b;
[Code] .......
View 9 Replies
View Related
Sep 22, 2013
i was suppose to write a program that gives a divide error but output is not showing anything like it.I am using orwell devc++ 5.4.2 and all the default built tools(gcc compiler etc.) with default settings.so, what should i do to see this error?
Code:
#include<stdio.h>
#include<conio.h>
int main()
{
int a,b,c;
float x;
}
[code]....
View 9 Replies
View Related
Mar 13, 2013
I need to write a program that accepts a number from the user, divides that number by two until it reaches one or cannot be divided evenly anymore, then adds all of the quotients from each division and displays them.
So something like this should be displayed:
Please enter a number: 8
8/2=4
4/2=2
2/2=1
4+2+1= 7
I thought about using an array to possibly store the quotients but I just can't see how that would work.
View 5 Replies
View Related
Jan 25, 2013
Whats the symbol for divide In C++
View 10 Replies
View Related
Feb 26, 2013
I am new to C programming and I am just wondering how to multiply / divide two different variables which the user type in as the promt is asking like this:
Code:
void inmatning3 (double *a, double *b) {
printf("Mata in tv217 stycken flyttal: "); /* asks you to type in 2 numbers */
scanf("%lf %lf", a, b);
}
When you've enterd the two numbers I need to eather multiply or divide the two variables "a" & "b" .....
View 5 Replies
View Related
Feb 12, 2014
Code:
#include <stdio.h> Code: #include <string.h>
int main() {
int i;
[Code]....
need to make triplet for the output
View 11 Replies
View Related
Jan 20, 2014
Code where the user wants to divide a number until it reaches to zero?
View 5 Replies
View Related
Nov 10, 2014
I need to make a program that takes in a user’s number and keep dividing it by 2 until it is 1.I need to display the division steps ...
I do not know how to keep making it divide by 2 until 1. I tired this but it obviously did not work..
Code:
#include <iostream>
using namespace std;
int main() {
//User Input
int input,total;
cout <<"Enter A Number To Be Divided By 2 Until 1" << endl << endl;
[Code] ....
View 7 Replies
View Related
Mar 20, 2013
I'm practicing so I wrote this simple program that suppose to add and divide two numbers. It does that but the result comes out with a 0 at the front and don't know why.
#include<iostream>
using namespace std;
int main()
{
int a ,b;
int result;
a = 0;
b = 0;
[Code]...
View 4 Replies
View Related
Jan 16, 2015
I am trying to do this:
Read a text file which is like:
2
2 10 1 2 7
3 8 3 7 7 10 7
The first line indicates how many paths exist in my design.
The second and third lines are those lines(the first number indicates 2 pairs, so 10,1 and 2,7 are 2 pairs)
The first element in the third line indicates 3 pairs and the pairs are 8,3 and 7,7 and 10,7.
I have been able to read the text file and store these into ONE array. However, I need to divide them up and have each line in a seperate array.
So for example
array alpha to contain 10,1 and 2,7
array beta to contain 8,3 and 7,7, and 10,7
How would you do this?
View 4 Replies
View Related
Mar 28, 2014
I want to multiply and divide 2 string that contains number. For example
s1=4 and s2=8 s1*s2=32 ,s2/s1=2
I don't want convert string to integer and do it.because i deal with big numbers.and these should be in string form.
View 1 Replies
View Related
Jan 31, 2013
I am trying to get a remainder of a number with a = 0.9144, rm = ry % a; however, I keep getting a divide by zero error (I believe due to the program rounding 0.9144 down to the integer 0).
View 2 Replies
View Related
Mar 8, 2013
Question about instead of using the division operator to display the output of user"s input....
View 4 Replies
View Related
Feb 27, 2014
For example, to calculate GPA, we add all the grade point of a course and divide by the number of courses we are taking. How do I create a program in such a way that when the user enters grade point for two courses, it will add the grade points for the two course and divide by 2. And if another user enters grade points for six courses, it adds the grade points and divides by 6.
View 2 Replies
View Related
Feb 28, 2013
The problem is that you have a set of numbers and you need to divide that set into two subsets where the difference between the sums of the subset is minimal.
Example: a set of numbers {1,5,9,3,8}, now the solution is two subsets, one subset with elements {9,3} and the other {8,5,1} the sum of the first one is 13 and the sum of the second is 13 so the difference between the sums is 0. The result shows the difference between the sums.
Another example: a set of numbers where the difference between the subsets cannot be zero, {9 51 308 107 27 91 62 176 28 6}, the minimal difference between the two subsets is 2.
I want to know how the function finds the two subsets, it works great because I've tested it for up to 300 inputs which sum adds up to 100,000.
Code:
#include <iostream>
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <limits.h>
using namespace std;
int BalancedPartition ( int a[] , int n ) {
[Code] ....
View 3 Replies
View Related
Oct 7, 2012
Create a program that adds, subtracts, multiplies, or divides two integers. The program will need to get a letter (A for addition, S for subtractions, M for multiplication, or D for division) and two integers from the user. If the user enters an invalid letter, the program should display an appropriate error message before the program ends. If the letter is A (or a), the program should calculate and display the sum of both integers. If the letter is S (or s), the program should display the difference between both integers. When calculating the difference, always subtract the smaller number from the larger one. If the letter is M (or m), the program should display the product of both integers. If the letter is D (or d), the program should divide both integers, always dividing the larger number by the smaller one."
And here is the test data. I am posting the results from my desk-check table.
operation first integer second integer answer
A 10 20 30
a 45 15 60
S 65 50 15
s 7 13 6
G -1
M 10 20 200
d 45 15 3
d 50 100 2
Then, I transferred my program into a source file. Here it is:
//Exercise16.cpp - display answer of two integers
#include <iostream>
using namespace std;
int main() {
//declare variables
int firstInteger = 0;
[Code] ....
After putting in the data, everything worked fine, except the last two operations, which are M (multiplication) and D (division). All the answers for the last two operations essentially give me a 0.
View 4 Replies
View Related
Apr 9, 2014
I am writing a simple console-based tic-tac-toe game. I am trying to write a function to check whether someone has won the game.
The board data is saved in an array called board: Code: int board[3][3] with each element corresponding either to an empty spot, an X, or an O, using the numbers 0, 1, and 2, respectively. For clarity:
Code:
#define EMPTY 0
#define X 1
#define O 2 Here is my function: Code: int check_state(int board[3][3]) {
int winner = 0;
[Code].....
View 1 Replies
View Related
Mar 7, 2013
A few days ago I got a "bright idea" to see if I could match a string, with an arbitrary length from 1 to 12, to its formulated sequence by using an algorithm to find all possible combinations of the integer combinations from 0 to 9 at each length (1 to 12).
Example: Desired numerical combinations from integers 1 to 3:
At Length 1:
1, 2, 3
At Length 2:
11, 12, 13, 21, 22, 23, 31, 32, 33
At Length 3:
111, 112, 113, 121, 122, 123, 131, 132, 133, 211, 212, 213, 221, 222, 223, 231, 232, 233, 311, 312, 313, 321, 322, 323, 331, 332, 333
And so on until the nth length (in my case a length of 12).
First off, I would like to say that this is not as easy as I thought. I clearly underestimated the problem seeing as I've spent hours attempting to write a working algorithm, but feel like I've made no progress.
Here are a few of my attempts:
Attempt 1:
#include <iostream>
#include <algorithm>
#include <math.h>
#include <string>
[Code]....
I can't exactly explain this one. It works if the length is 2 or less; however, the order of the output is horrendous.
Attempt 3: I tried using recursion, but only found myself getting more and more lost the further I tried developing my function. Cannot find my work for this attempt.
I would really like to figure this out on my own, but I am very stuck as you can see. I also lack time that I can spend working on this since im a full time student.
View 14 Replies
View Related
Jul 24, 2013
I wrote a version of find_all() which searches through containers for matches and returns another container that holds iterators pointing to each match. When I compared what I wrote to what the authors of Professional C++ wrote, I found the two find_all() functions to be very different.Here they are:
//Mine
template<typename Iterator, typename Predicate>
std::list<Iterator> find_all
(Iterator front, Iterator back, const Predicate& match) {
std::list<Iterator> toreturn;
for(; front != back; ++front) if(*front == match) toreturn.push_back(front);
[code]...
View 5 Replies
View Related
Jun 27, 2014
I have VRP algorithm written in C++, run with command prompt Windows. input (command line) -> VRP.exe -> output (txt file)
Now I want to built a website to run it as SaaS. How to run an exe application(compiled c++) in server used as Saas? What kind of programming do I have to use? Ruby or html5 or others? I don't know how to start.
View 3 Replies
View Related
Aug 27, 2013
i was trying to achieve a better way of game looping. so i have a game loop algrithm. but i dont think its quite good. i want it to be better.
here is my algorithm
bool quit = false;
while(quit == false) {
while(blah blah events)//here we hold events {
if(the user want to quits) {
quit = true;
[code]....
View 2 Replies
View Related
May 12, 2014
To construct and write down algorithm of determination of the sum of squares of consecutive integers with recursion use. I tried to do something:
public static int RecSumSquare(int x, int n)
{
if (n < 0) throw new ArgumentException("n should be greater than zero");
if (n == 0) return 0;
else return x*x+RecSumSquare(x, n - 1);
}
But I don't know as the beginning and the end of this algorithm will look.
View 2 Replies
View Related
Mar 10, 2014
I'm still a beginner at C++ programming, I have tried to implement some optimization algorithms (related to database) in C++, I cannot say it is going as far as I thought it will be, some errors does not even make sense, I will cut to the chase, I need to implement SA (Simulated Annealing) in C++, SA, which is an example of the Randomized Algorithms, functions on the concept of randomness and probability. In addition, I searched the internet with no (let say "accurate") code that might give me insight on how to begin, I only found one code for SA that I could understand, and edited it (which will be shown below), still looking to enhance and correct it more.
The code for SA is as follows:
Code:
#include "stdafx.h"
#include <iostream>
#include <time.h>
#include <stdlib.h>
#include <cmath>
[Code] .....
If the concept of Simulated Annealing is not clear, you may refer to the following papers which can be found in a simple Google search: Simulated Annealing Algorithms: an overview.An Introduction to Interacting Simulated Annealing. Query Optimization (there is a sub-section for Simulated Annealing in this paper that explains SA briefly) ....
View 6 Replies
View Related
Sep 17, 2014
I'm trying to draw and pivot a rectangle. How to get the new coordinates if I turn it 90 degree for example?
From this:
To this:
This is not good because it will reduce it in size.
Code:
int size = 50;
if(b) // draw rectangle
{
POINT points[4] =
{{xCoord, yCoord},
[Code] ....
View 7 Replies
View Related
Jun 3, 2013
Trying to multiply to matrixes using the following algorithm,
Code:
ABrec(A,B)
n=A.rows; //n must be multiple of 2
C is a new n*n matrix.
if(n==1)
C[0][0]=A[0][0]*B[0][0];
[Code] ....
but the code doesn't work !!!
View 4 Replies
View Related