My for loop stops at the 88th number which I find very odd. I cannot figure out why it is doing this. It doesnt exit the for loop and it doesnt continue reading any numbers. Im very confused.
#include <iostream>
#include <math.h>
#include <vector>
#include <algorithm>
using namespace std;
double distanceCalc(double x1, double y1, double x2, double y2)
{
return sqrt(((x2-x1) * (x2-x1)) + ((y2-y1) * (y2-y1)));
}
int main()
{
int n;
double x, y, min = 10000000;
vector<double> xCoor;
vector<double> yCoor;
while (cin >> n && n != 0)
{
int numPairs = n;
cout << numPairs << endl;
for (int i = 0; i < numPairs; i++)
{
cin >> x >> y;
cout << x << " " << y << " " << i << endl;
xCoor.push_back(x);
yCoor.push_back(y);
}
if (n == 1)
{
min = 1000000000;
}
for (int i = 0; i < n; i++)
{
for (int j = i + 1; j < n; j++)
{
double temp = distanceCalc(xCoor[i], yCoor[i], xCoor[j], yCoor[j]);
if (temp < min)
{
min = temp;
}
}
}
(min < 10000) ? printf("%.4f\n", min) : printf("INFINITY\n");
}
return 0;
}
the input im using is: https://drive.google.com/open?id=1SNZAVh2lkiih-RSQRa9nH5Oj6RD-fw5Z
Both I and others have tried your code, and have not been able to reproduce the problem.
It is of course possible that there is something wrong with your C++ installation, but a more common problem when these things happen is that you are running an older version of the program than the one you think you are running. So check carefully that you are actually re-compiling the code after each change, and that you are really using the source file that you think. You could, for example, add an extra printout to the program, and see if you find it in the output.
Also check that you are using the input file you think you are using. Perhaps you are really reading another version of the input, and that one has some garbage after the first 88 numbers?
Related
So this is what I have
#include <iostream>
using namespace std;
int createArray(){
int array[10][10] = {0};
int x, y;
int size = 0;
while(x != 0 && y !=0){
cin >> x >> y;
while( x<0 || x>10 || y<0 || y>10){
cout << "error" << endl;
cin >> x >> y;
}
if(x>0 && y >0){
array[x-1][y-1] = 1;
}
if(size < x){
size = x;
}
if (size < y){
size = y;
}
}
return(array, size);
}
int printArray(int array[10][10],int size){
int i, j;
for(i=0; i<size; i++){
for(j=0; j<size; j++){
cout << array[i][j] << " ";
}
cout << endl;
}
return 0;
}
int main() {
/*
What happens here? I thought I could do something like..
auto [arrayA, sizeA] = createArray();
printArray(arrayA, sizeA);
but that's not working
*/
}
I've been messing around for the last few hours, and it hasn't been working. I wouldn't be surprised if there is something sloppy left over in the code from my different attempts, but I am a little brain fried, ha! This is an assignment for school, and I have a lot more functions to make work similarly, but I can't seem to make it happen. I can write them straight through in main() and it works, but I specifically need to do it with functions.
you cannot do this in c++
return(array, size);
well you can, but it doesnt do what you think. It just returns the size (lookup up comma operator)
Many many times in c++ assigments there are stupid rules about what you can and cannot use.
You can used std::pair to return 2 things.
I would use a vector of vector rather that the fixed size array
I'm currently practising the kickstart coding challenges by google. here is a link to see the question :
https://codingcompetitions.withgoogle.com/kickstart/round/0000000000050e01/00000000000698d6
I have made the program which succeeded giving the right answer using the sample question which the question provided but when I actually submit it for checking, the code met a runtime error. Since it won't give why so, I have no idea how to proceed. So I hope you guys can help...
Here is the code:
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
int ts;
cin >> ts;
for (int i = 1; i <= ts; ++i)
{
int n, p;
cin >> n >> p;
// input skill level and sort descending
int skill[100] = {0};
for (int i = 0; i < n; i++) cin >> skill[i];
int size = sizeof(skill) / sizeof(skill[0]);
sort(skill, skill + size, greater<int>());
int ans= 9999999;
for (int i = 0; i <=(n-p); i++)
{
int tmp = 0;
for (int j = i + 1; j <= i + (p - 1); j++)
tmp += skill[i] - skill[j];
ans = min(ans, tmp);
}
cout << "Case #" << i << ": " << ans << endl;
}
}
So the question says that N can be upto 10000, but your code assumes that it is no bigger than 100.
Regarding LTE, you are almost there. Try to replace the
for (int j = i + 1; j <= i + (p - 1); j++)
tmp += skill[i] - skill[j];
loop with the constant time expression. Hint: when the most skillful player leaves the window, by how much the training time for the rest players gets decreased?
Would like to seek a bit of help from StackOverflow. I am trying to print out the sequence of Fibonacci number and also the number of time the iterative function is called which is supposed to be 5 if the input is 5.
However, I am only getting 4199371 as a count which is a huge number and I am trying to solve the problem since four hours. Hope anyone who could spot some mistake could give a hint.
#include <iostream>
using namespace std;
int fibIterative(int);
int main()
{
int num, c1;
cout << "Please enter the number of term of fibonacci number to be displayed: ";
cin >> num;
for (int x = 0; x <= num; x++)
{
cout << fibIterative(x);
if (fibIterative(x) != 0) {
c1++;
}
}
cout << endl << "Number of time the iterative function is called: " << c1 << endl;
}
int fibIterative(int n)
{
int i = 1;
int j = 0;
for(int k = 1; k <= n; k++) {
j = i + j;
i = j - i;
}
return j;
}
First, initialize the variable
c1 = 0;
so that you will not get any garbage value get printed.
Secondly this:
if (fibIterative(x) != 0)
{
c1++;
}
will make 2*count - 1 your count. You don't need that.
Edit: I have noticed that you have removed extra c1++; from your first revision. Hence, the above problem is not more valid. However, you are calling the function fibIterative() again to have a check, which is not a good idea. You could have simply print c1-1 at the end, to show the count.
Thirdly,
for (int x = 0; x <= num; x++)
you are starting from 0 till equal to x that means 0,1,2,3,4,5 total of 6 iterations; not 5.
If you meant to start from x = 1, you need this:
for (int x = 1; x <= num; x++)
{ ^
cout << fibIterative(x) << " ";
c1++;
}
I have coded this program and it works fine. I get the result I want but because we are using an old system to submit it, my code is rejected because it saying that the 3 last lines generate a blank of my code. Can someone please tell me where is the problem and how I can fix it? Thank you!
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int i, row_nr;
cin >> row_nr;
if(row_nr > 1 && row_nr <= 30)
for(i = 1; i <= row_nr; i++)
{
for(int j = 0; j < row_nr; j++)
{
cout << i + j * (row_nr);
{
cout << " ";
}
}
cout << endl;
}
return 0;
}
You're outputting a space after every value, so there is going to be a space at the end of each line. You should add a check so that you don't output a space after the last value of each line. It seems like you might have intended to do this, but forgot to write the if statement.
#include <iostream>
//#include <iomanip> why?
using namespace std;
int main()
{
int row_nr;
cin >> row_nr;
if(row_nr > 1 && row_nr <= 30)
for(int i = 1; i <= row_nr; i++) //declare iterator variable in for loop statement
{
for(int j = 0; j < row_nr; j++)
{
cout << i + j * (row_nr);
if(j < row_nr - 1) //you forgot this line
{
cout << " ";
}
}
cout << '\n'; //endl flushes the buffer, unnecessary here
}
return 0;
}
I'm trying to solve the 3n+1 problem using VS2010 c++,in small inputs it works well,but when it reaches 113383 it overflows.
Here is the problem link.
This is the code I'm using to solve this problem :
#include <iostream>
using namespace std;
int main(void) {
while (!cin.eof()) {
int i, j, maxCycle = 0, tmaxCycle = 0;
cin >> i >> j;
for (int x = i; x <= j; x++) {
int n = x;
tmaxCycle = 0;
while (n != 1) {
if ((float)(n/2) != (n/2.0)) {
n = 3*n + 1;
}
else {
n /= 2;
}
tmaxCycle += 1;
if (n < 0) {
int blah = 0; //just for the breakpoint
}
}
tmaxCycle += 1;
if (tmaxCycle > maxCycle) {
maxCycle = tmaxCycle;
}
}
cout << i << "\t" << j << "\t" << maxCycle << endl;
}
system("pause");
}
I made a breakpoint at line 15,and in this point values overflows
n=-1812855948
Use 64-bit unsigned integers. If those overflow, use a bignum library like the GNU Multiple Precision Library. Bignums give you unlimited precision and size.
This
if((float)(n/2)!=(n/2.0))
produces incorrect results, long before int overflows. Change it to
if ( n & 1)