C++ :: Greatest Common Divisor Of Two Numbers
Nov 19, 2014How do I get the greatest common divisor of two numbers in C++?
View 1 RepliesHow do I get the greatest common divisor of two numbers in C++?
View 1 RepliesI have written the following function to calculate GCD of floating point numbers, but when I run this for (111.6, 46.5), the calculation of fmod(a,b) in the funciton starts giving the wrong result after 2 recursive calls. I am unable to find the error here.
float gcd(float a, float b){
if (a>b) {
if(b==0){
[Code]....
Is there a function or algorithm in stl in c++ the gcd of a vector ?
View 1 Replies View RelatedHow can I calculate GCF of many numbers? I thought I could calculate two by two numbers, but it not seems to be a very effective idea. There is my function:
int gcf (unsigned int x, unsigned int y)
{
return (y == 0) ? x : gcf (y, x % y);
}
#include<iostream>
using namespace std;
int GCD (int num , int x ) {
for ( int m = x ; m <= num ; m++ ) {
[Code] ....
I have to write a program that sorts numbers from least to greatest. The way that it has to sort them is:
1) The program assigns the first number to be a minimum
2) If the next number is less than the minimum is now that number and they switch places. (We keep on looking if the number next to it is not smaller)
3) The program also gets the index of the minimum number
4) We keep on going until it is in order.
// Sort.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include <vector>
using namespace std;
void Sort(vector<int>&input);
int _tmain(int argc, _TCHAR* argv[]){
vector <int> input;
[Code] ....
I have a class 'A' which is almost perfect for my needs. Class 'B' uses class 'A' I've now designed Class 'C' and Class 'D' and noticed that there is a good chunk of code in class 'B', 'C' and 'D' for using Class 'A' is duplicated. I've separated out this code in specific, standalone functions in each of the classes. Now I'm wondering where this code should go. At the moment, the functions are duplicated in the three calling classes (B, C and D). Placing the functions into class 'A' would break the single responsibility principle. Inheritance to add functionality would likely break both SRP and LSP. The one that seems that it may work is composition.
However, Is designing a complete class just for a few functions over kill?
Would it be valid for classes 'B', 'C' and 'D' to access both the new class 'E' (which would depend on A) and the old class 'A' (which would have to be the same instance as the instance in the new class 'E'), or should the new class 'E' provide sufficient functionality so that Classes B, C and D don't need to access Class A directly? It would seem that its then an incomplete interface of the original object with additional functionality (ie, incompatible) Or should I do it a completely different way?
The problem says that i have a number n > 0 and the program should determinate the number between 2 and N who has the maximum divisor sum (1 and N wont count as divisor). If i have more numbers with maximum divisor sum, should wrote only first number. Exemple: for N = 100 , will show 96 bcoz he has the maximum divisor sum, which is 155. For the start i tried to show only the maximum divisor sum, not the number who has the maximum divisor sum with this code:
#include<stdio.h>
int main() {
int n,s=0,i,m;
printf("Please introduce the number N > 0:");
scanf("%d", &n);
i = 2;
[Code] .....
And it shows me 116, but the correct results is 155. Which is the problem ?
Create a program that will ask the user to input 10 integers of an array the program must write the index of the greatest element of the array
target output
input the integers
the greatest element is 5 and the index is 7
My homework assignment is to compare the id's of students from least to greatest. How to do this.
// Student Class.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
[Code].....
Develop a function that finds the greatest difference between two consecutive elements stored in an array. Develop a 9 element array of doubles to test your code. Print to the screen which two numbers have the greatest difference as well as the value of the difference. Finally include an overloaded version of the function that will work if the array is composed of integers. Include your code used to test this function.
View 8 Replies View RelatedSo I'm trying to make two arrays, then the third array that will include common elements of those two arrays. I did make a code for that, but the problem is I don't know how to include that the elements do not repeat.
For example, if there are two arrays,
Elements of the first one: 2, 3
Elements of the second one: 2, 3, 2
The third array is going to be: 2 2
While I want it to be only 2.
#include <iostream>
using namespace std;
int* intersection(int* n1, int d1, int* n2, int d2, int& d3) {
d3=0;
for (int i=0; i<d1; i++) {
[Code] ....
I am trying to take a string that is within the main function, and write a void function that gives me the most common alpha character used inside the string. How to mix a string and an array together like that as I am not too familiar with arrays yet.
View 8 Replies View RelatedIn 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].....
I have been struggling with this program. I am somewhat new to c and suck at logic. I have a personal program I want to make that I will try to get extra credit for in school. I have a printed set of winning lottery numbers form the last 10 years. I chose the easiest one do do logically which is just 5 numbers none repeating.
I am trying to find out how I can print the least common 10 sets. I think if there are any set which have not been picked I would have to print all of those because logically they would all be equal, then print sequentially the sets least picked up to 10.
I have pseudocode which I am sure is wrong but will post it just to show that I am trying. My first attempt was to add the numbers but quickly realized that that wouldn't work ...
5 Nums Pseudocode
Code:
Read Nums
Parse Into Ints
Make Array [185] //39+38+37+36+35 The highest the numbers added together can go
//LOGIC
[Code] ....
how assignments is to write code that returns a value that is repeated more than once in an array of 8 integers.For example:
Numbers: 30 12 100 33 2 2 1 40
Output: 2
Here is my code so far:
Code:
#include <stdio.h
int main(void){
int i;
int j;
int arr[8];
int count;
int maxCount = 0;
int maxValue = 0;
[code]....
When I compile this code (on Vim), I get an error when I print the array of 8 above (see ERROR above).
Is there a way to get the URL when the user opens a web page with a common web browser?
View 3 Replies View RelatedHow you would overload an operator for Common Data-types like "char" and "int".
I often use bool arrays to create a multilevel-trigger-systems, when iterating over multiple containers or waiting for two events to occur at the same time.
For example:
I would define..
bool trigger[2] = {0, 0};
And when doing work via a loop, I use it like so:
while(trigger[0] != 1 && trigger[1] != 1)
You can probably see where I'm going with this. I want to be able to use my bool array with the "!" operator.
So if "trigger == 0" (as a whole), it returns false.
How can I achieve this?
Can you create custom operators? Say if I wanted to create "or-gates" or "xor-gates" etc.
If I have number of arrays (its 3 for instance) with fixed size (15), it consist of zeros and non-zeros
eg:array1[15]={5,5,0,0,4,4,4,0,0,0,1,0,0,0,3}
array2[15]={1,0,0,0,0,7,7,0,0,3,0,0,0,0,2}
array3[15]={6,6,6,0,8,8,8,0,0,0,3,3,0,0,4}
...........
sample output for the above arrays:
Index Nim_of_zeros
3 1
7 2
12 2
How can I count the common zero sequences and there indexes of arrays?
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?
If I have number of arrays(is 3 for instance and may vary) with fixed size (15), it consist of zeros and non-zeros
eg:array1[15]={5,5,0,0,4,4,4,0,0,0,1,0,0,0,3}
array2[15]={1,0,0,0,0,7,7,0,0,3,0,0,0,0,2}
array3[15]={6,6,6,0,8,8,8,0,0,0,3,3,0,0,4}
...........
How can I count the common zero sequences and there indexes for all arrays?
sample output for the above arrays:
Index Nim_of_zeros
3 1
7 2
12 2
we conform to the ISO C standard and this snippet of code : Code: vector<tree*> *leaves = new vector<tree*>[num_threads]; where num_threads is specified from command line arguments so not dynamically allocating it violates the standard.
Let's also assume num_threads is greater than one.
What I want to do is scan each vector in leaves for duplicates. If any two vectors in the set have matching addresses, they both immediately go onto the "unsafe" pile and will no longer be subject for testing.
If a vector clears one vector, we test it against the others in the set.
So if we have 3 vectors, A, B and C we test A against B then A against C. For efficiency, we then then just test B against C.
Like I said, I want a "safe" and "unsafe" pile. Every vector in "safe" is fully unique while every vector in "unsafe" is not unique.
I thought about just using a for-loop to loop through leaves and then iterate through each element but I'm not sure if that'll work just right out of the box.
This is what I try to achieve:
1. Using a CScrollView as the View class type.
2. Using Document/View Architecture.
3. Insert multiple Common Controls such as CEdit, CButton, CListCtrl into the View dynamically.
4. All the Common Controls is base on a template created by user. So I do not know the quantity of the Common Controls.
This is the plan on how to do it:
1. In View OnInitialUpdate(), read the template to get all necessary info on the Common Controls to be create.
2. In View OnInitialUpdate(), create all Common Control. Some Common Control may not have WS_VISIBLE as it may not needed at initial stage.
3. In View OnUpdate(), I need to refresh the Common Control properties such as is its text, position or its visibility. This should happen if Document call UpdateAllView().
My Question:
1. Is this a normal & useable plan?
2. Do I miss other functions for any other messages?
3. Still wondering if I need to involve any coding in OnDraw() for this plan.
If I have number of arrays(its 3 for instance and may vary) with fixed size (15), it consist of zeros and non-zeros
How to write a program to count the common zero sequences and there indexes of arrays?
eg:array1[15]={5,5,0,0,4,4,4,0,0,0,1,0,0,0,3}
array2[15]={1,0,0,0,0,7,7,0,0,3,0,0,0,0,2}
array3[15]={6,6,6,0,8,8,8,0,0,0,3,3,0,0,4}
...........
sample output for the above arrays:
Index==> Nim_of_zeros
3==> 1
7==> 2
12==> 2
I have a dynamic number of lists coming in from a different method . i need to find out the common elements present in all of the lists .
How do i do it with intersect construct in C#.
Placing it in a loop gets me the elements common in the last comparison , but never gets me elements commong in all lists.
I'm trying to write a function that takes two linked lists and creates a third one with only the common elements.
It assumes the first list (the caller) has no dups, but it doesn't seem to be working. The program doesn't crash, it just hangs when it is supposed to display L3 (the third list)..everything else runs and is displayed fine.
template <typename T>
LList <T> LList <T>:: common (LList <T> &B)//common fct
{
Node <T> *hunter1 = Head;
[Code]......