动态数组和函数
我正在尝试执行我的项目,并且卡住了。如果我理解,我的教授希望我使用动态数组,并且有一个函数可以比较整数并得到它们的GCD。我不能让这个功能起作用。有什么想法吗? 这里是舞会:动态数组和函数
写一个程序来计算任何有限整数集的最大公约数。使用函数来计算GCD。该集合中的元素数量不应该预先确定。当您输入数据时,您需要编写将要计数的代码,集合中有多少个数字。以欧几里德算法为基础。
我输入10,100和40 GCD应该是10,但是,我得到这个结果:
The GCD of: is:
10 0
100 0
40 0
#include <iostream>
#include<iomanip>
using namespace std;
int greatestdivisor(int b[], int size); /*Write prototype for gcd */
int main()
{
int greatest;
int max=1;
int* a= new int[max]; //allocated on heap
int n=0;
cout<<"Input numbers: "<<endl;
cout<<"Hit Enter key after each input and type any letter to finish"<<endl;
while(cin>>a[n]){ //read into array
n++;
if(n>=max){
max=n; //increase size of array
int* temp = new int[max]; //creates new bigger array
for(int i=0;i<n;i++){
temp[i] = a[i]; //copy values to new array
} //end for
delete [] a; //free old array memory
a = temp; //a points to new array
} //end if
} // end while
cout<<endl;
greatest = greatestdivisor(a, max);
cout<<"The GCD of: "<<" is: "<<endl;
for(int j=0;j<max;j++)
cout<<setw(5)<<a[j]<<setw(10)<<greatest<<endl;
n++;// prints elements of array and call function
} // end main
// gcd finds greatest common divisor of array
int greatestdivisor(int b[], int size)
{
int greatest =1;// current greatest common divisor, 1 is minimum
for (int x=0; x<=size; x++) {
int m=b[x];
int r=2;
if(m%r==0){
greatest =m; // update greatest common divisor
} //end if
} // end for
return greatest; //return gcd
} // end fuction gcd
有在你的代码中的许多问题, try this
并找出你在做什么错:
#include <iostream>
#include<iomanip>
using namespace std;
int greatestdivisor(int b[], int size); /*Write prototype for gcd */
int main()
{
int greatest;
int max=1;
int* a= new int[max]; //allocated on heap
int n=0;
cout<<"Input numbers: "<<endl;
cout<<"Hit Enter key after each input and type any letter to finish"<<endl;
while(cin>>a[n]){ //read into array
n++;
if(n>=max){
max=n+1; //increase size of array
int* temp = new int[max]; //creates new bigger array
for(int i=0;i<n;i++){
temp[i] = a[i]; //copy values to new array
} //end for
delete [] a; //free old array memory
a = temp; //a points to new array
} //end if
} // end while
cout<<endl;
greatest = greatestdivisor(a, n);
cout<<"The GCD of: "<<" is: "<<endl;
for(int j=0;j<n;j++)
cout<<setw(5)<<a[j]<<setw(10)<<greatest<<endl;
} // end main
int gcd(int a,int b)
{
int t;
while(a)
{
t = a;
a = b%a;
b = t;
}
return b;
}
// gcd finds greatest common divisor of array
int greatestdivisor(int b[], int size)
{
int greatest =b[0];// current greatest common divisor, 1 is minimum
for (int x=1; x<size; x++) {
greatest = gcd(greatest, b[x]); // update greatest common divisor
} // end for
return greatest; //return gcd
} // end fuction gcd
您的GCD算法已损坏。它应该从前两个条目开始,找到数组中每个连续值的GCD。对于数组中的所有条目都重复,最终的gcd在所有这些条目中都是通用的。正如在评论中提到的,你的(破损的)gcd迭代算法的大小也是错误的;它应该是严格的小于。
一个备受精简版本是这样的:
#include <iostream>
#include <iomanip>
#include <cmath>
static int gcd(const int b[], size_t size);
int main()
{
int* a = nullptr, value=0;
size_t n = 0;
std::cout<<"Input numbers:\n";
while(std::cin >> value)
{
int *temp = new int[n+1];
std::copy(a, a+n, temp);
delete [] a;
a = temp;
a[n++] = value;
}
std::cout<<"The GCD is " << gcd(a, n) << '\n';
delete [] a;
}
static int gcd(const int b[], size_t size)
{
int res = (size > 0 ? std::abs(b[0]) : 0);
for (size_t x=1; x<size; ++x)
{
int n = std::abs(b[x]);
while (n > 0)
{
auto tmp = res;
res = n;
n = tmp % n;
}
}
return res;
}
输出
Input numbers:
10
100
40
x
The GCD is 10
使世界更美好:std::vector
现在你可以看到一个手动管理的动态数组是如何工作的,但我不能强调这可以简化多少,这可以通过而不是来完成,而是简单地使用标准库中的预设功能。 std::vector
和std::istream_iterator
将对此任务做出简短的工作,并且代码变得非常不容易出错。您可以从std::vector
获得动态内存管理,并使用std::istream_iterator
将格式化的输入复制到EOF或非int
数据。总之,几乎所有的数据管理方式都是为您处理的。
请看:
#include <iostream>
#include <vector>
#include <iterator>
#include <iomanip>
#include <cmath>
static int gcd(const int b[], size_t size);
int main()
{
std::cout<<"Input numbers:\n";
std::vector<int> a((std::istream_iterator<int>(std::cin)),
std::istream_iterator<int>());
std::cout<<"The GCD is " << gcd(a.data(), a.size()) << '\n';
}
static int gcd(const int b[], size_t size)
{
int res = (size > 0 ? std::abs(b[0]) : 0);
for (size_t x=1; x<size; ++x)
{
int n = std::abs(b[x]);
while (n > 0)
{
auto tmp = res;
res = n;
n = tmp % n;
}
}
return res;
}
输出是和以前一样。祝您好运
这很好 - 我不知道istream迭代器。 +1 – chrisb2244 2014-11-01 07:45:05
如果问题完全按照您所描述的方式指定,那么看起来array
并不是明确需要的。
因此,您可以简化到像
#include <vector>
#include <iostream>
#include <sstream>
int greatestdivisor(std::vector<int> &ints);
int euclid(int a, int b);
int main()
{
std::vector<int> listOfInts;
std::string line = "default";
int tempInt=0;
std::cout << "Description" << std::endl;
while (line.length() != 0)
{
std::getline(std::cin, line);
std::stringstream temp(line);
temp >> tempInt;
listOfInts.push_back(tempInt);
}
listOfInts.pop_back(); // Remove the last entry, which is counted twice by this while loop :/
for (int i=0; i< listOfInts.size(); i++)
{
std::cout<< listOfInts[i] << std::endl;
}
int gcd = greatestdivisor(listOfInts);
std::cout << "gcd = " << gcd << std::endl;
}
int greatestdivisor(std::vector<int> &ints)
{
int currentGCD = ints[0];
while (ints.size() > 0)
{
int a = ints.back();
ints.pop_back();
currentGCD = euclid(a, currentGCD);
std::cout << "currentGCD = " << currentGCD << std::endl;
}
return currentGCD;
}
int euclid(int a, int b)
{
if (b == 0)
return a;
else
return euclid(b, a % b);
}
'我不能让功能工作。'你的程序出了什么问题,请提供更多细节。 – user657267 2014-10-31 06:02:28
当我运行它时,它显示不正确的信息。它应该显示10与我输入的数据。 – Meeeeee 2014-10-31 06:06:35
如果您正在拍摄gcd算法,您的'mostdivisor'函数似乎不会做太多工作,而且它的测试条件应该是'x
WhozCraig
2014-10-31 06:07:34