C/C++ :: Greatest Common Factor Of Many Numbers
Jun 15, 2014
How 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);
}
View 3 Replies
ADVERTISEMENT
Nov 19, 2014
How do I get the greatest common divisor of two numbers in C++?
View 1 Replies
View Related
Dec 9, 2013
#include<iostream>
using namespace std;
int GCD (int num , int x ) {
for ( int m = x ; m <= num ; m++ ) {
[Code] ....
View 1 Replies
View Related
Jan 12, 2013
I 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]....
View 1 Replies
View Related
Jan 2, 2015
Is there a function or algorithm in stl in c++ the gcd of a vector ?
View 1 Replies
View Related
Jul 5, 2014
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] ....
View 4 Replies
View Related
Apr 13, 2014
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?
View 4 Replies
View Related
Feb 12, 2015
So I'm doing this problem where the user will input a number that will be factored until the number is 1, 2 people will play this game and they will take turns entering a number that the initial number(named gameN) will be divided by until it is 1. Whoever get it to 1 wins. My problem is I'm not really sure how to do this, I'm pretty new to programming so besides some if statements and a loop, the problem shouldn't require too much to write. Some rules for the game are, the number entered to factor the gameN cant be less than 2, and has to divide evenly into gameN, which I have signifies with modulus. Here is where I'm at so far.
#include <stdio.h>
int main(){
int gameN;
int p1f;
int p2f;
printf("What number should the game be played with?");
scanf("%d", &gameN);
[Code] ....
View 2 Replies
View Related
May 1, 2013
Write a C-program that efficiently multiplies a number by a factor 2 to the power n. The number to multiply and n are variables, which get a value at the start of the program.
Clue: 1 shift to the left is the same as multiplying by 2. 2 shifts to the left are the same as multiplying by 4. 3 shifts to the left are the same as multiplying by 8
View 7 Replies
View Related
Feb 4, 2014
How will you factor out data with different values to match to another value.
For example:
value A=100
value B=50,10,20,5,15,20,30
How will you construct a logic that the values on value B will have an equal value as value A.
100 = 50+20+10+20 or 100 = 50+30+20 and so on and so forth.
I am stuck.
View 2 Replies
View Related
Feb 10, 2015
i want to write a program which find the biggest prime factor of a number for example the biggest prime factor of six is three or the biggest prime factor of fifteen is five. What is my program bug
Code:
#include <stdio.h> // main functions
#include <math.h> // for sqrt function
int main()
{
int i, j, k, f; // F = Flag;
printf("Enter K
[Code]...
View 9 Replies
View Related
Mar 10, 2013
I am trying to find the largest prime factor of a number. But when I am trying to determine if a number is a prime number in the function:
int is_prime(int number), I am unable to exit the loop.
Here is the code:
#include<iostream>
using namespace std;
int is_prime(int number) //the problem is in this function {
int num = number;
int factor=0;
do{
num++;
for(int i=1;i<(num+1);i++){
[code].....
So when the program runs, it first divides 20 by 2, to get 10, then divides 10 by 2 to get 5. Since, // condition 1 is not met, it passes 2 to the function int is_prime(int number). The function is able to return 3, but cannot exit the loop when num is 4.
I think the problem lies in the function: int is_prime(int number).
View 5 Replies
View Related
Feb 17, 2014
My assignment is to create a simple stock broker program that ask the user how much they are willing to invest and ask what company they would like to invest in. Finally it outputs how many shares the user will have based on their investment amount. My code is below. My professor said to declare symbolic constants and factor out the if else statements. Ive been struggling trying to understand constant variables. How do I use const variables to factor out the if else statements?
#include <iostream>
#include <cstdlib>
using namespace std;
int main() {
//Declare Variables
const double BAC = 16.7;
const double Citigroup = 49.52;
[code]....
View 4 Replies
View Related
Oct 10, 2013
I need codes for a program in C or C++ that will show the real factor (the smallest one, without the number 1) when an integer is given as input.
When the program is started, it will ask to enter a number and press enter. After entering a number, it will first check if it is a prime number or not. If prime, it will notice that it is a prime number, otherwise, it will print the smallest real factor of the given integer.
For example, if 12 is entered, it will print, the smallest real factor for this number is: 2
If 27 is entered, it will print, the smallest real factor for this number is: 3
...and so on
View 12 Replies
View Related
Oct 29, 2013
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
View 3 Replies
View Related
Jul 26, 2014
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].....
View 8 Replies
View Related
Mar 31, 2013
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 Related
Jan 23, 2015
So 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] ....
View 14 Replies
View Related
Oct 24, 2013
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 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
Nov 22, 2013
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] ....
View 5 Replies
View Related
Feb 23, 2013
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).
View 3 Replies
View Related
May 11, 2012
Is there a way to get the URL when the user opens a web page with a common web browser?
View 3 Replies
View Related
Aug 11, 2014
How 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.
View 3 Replies
View Related
May 12, 2014
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?
View 2 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