When I ran my solution to The Square Within from codercharts.com on my computer (Intel i5 newest version), I was able to build my program in 646 ms, but for some random reason I was able to enter the code for the input part of my code, but when I pressed "enter" to get results, the screen popped up with a Windows error:
Here is my code that I ran (logic all follows the problem I was given).
#include <iostream>
using namespace std;
int main (int argc, char ** argv)
{
int dimension[]={};
int result;
int counter = 0;
for (int i=0; i < 1000000; i++){
counter+=1;
}
for (int a=0; a<counter; a++){
result=(dimension[a]*(dimension[a] + 1)*((2*dimension[a]) + 1))/6;
}
while (true){
cin >> dimension[counter];
break;
cout << result << " ";
}
return 0;
}
Can something review my code and help me with this hard problem? Thanks in advance!
The line
int dimension[]={};
needs a value for its size:
i.e.
int dimension[1000001]={};
Related
I am a newcomer to C++. need to write a piece of code in C++, read the txt data, expand into one dimension (one row or one column) and sort in descending order. Finally I want to specify a percentage range to average. I found some code snippets on the web and debugged it like this, but in the sorting phase I can't get the results I want, all the data will be turned into -858993460. Hope someone can help me.
txtfile
#include "stdafx.h"
#include <vector>
#include<iostream>
#include<algorithm>
#include<stdio.h>
#include<fstream>
#include<typeinfo>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
int i = 0, j = 0, k = 0;
int t = 0, f = 0;
int data[20];
ifstream infile;
infile.open("C:\\Users\n\Desktop\1111.txt");
for ( i = 0; i < 5; i++)
{
for ( j = 0; j < 4; j++)
{
infile >> data[k++];
}
}
for (i = 0; i < 5; i++)
{
for (j = 0; j < 4; j++)
{
if (data[i] > data[j]) //Sort from big to small
{
//Exchange the values of both
t = data[i];
data[i] = data[j];
data[j] = t;
}
}
}
for (int f = 0; f < 20; f++)
{
cout << data[f];
cout << '\n';
}
system("PAUSE");
}
result
You need to escape your
\
If you change your
infile.open("C:\\Users\n\Desktop\1111.txt");
to
infile.open("C:\\Users\\n\\Desktop\\1111.txt"); it should work properly.
EDIT: I just reproduced your file and I can read all your inputs. I think there is still a problem with the opening process of your file.
Another problem: You read in 20 values into an array. Therefore you dont need 2 nested for loop. One loop which counts to 20 is enough to do that task. This is a main reason why your sorting algorithm wont work.
Some strange code but your main error is not checking that the file open has succeeded. The garbage values you are getting strongly suggests that the file open has failed.
Files can fail to open for lots of reasons (include the one that cKai mentioned above). Add the following to your code to check if the file has opened.
ifstream infile;
infile.open("C:\\Users\\n\\Desktop\\1111.txt");
if (!infile.is_open())
{
cerr << "file open failed, quitting\n";
system("PAUSE");
return 1;
}
EDIT
OK so the above didn't prove anything, now maybe try this. Change
infile >> data[k++];
to this
if (!(infile >> data[k++]))
{
cerr << "read failed, quitting\n";
system("PAUSE");
return 1;
}
and see if that produces any error messages.
I am trying to read a .ply file with c++ and save the geometric information in vectors (The border points are floats and the border triangles are int's. The code works under Linux but when I try to use it under Windows it doesn't behave as intended.
Here is a trimmed down version of the code:
#include <iostream>
#include <iomanip>
#include <fstream>
#include <vector>
using namespace std;
string FilenamePLY;
int NumberBorderPoint = 1572866;
int BorderNumberTriangle = 3145728;
char numFloat;
char numInt;
int main(int argc, char** argv)
{
FilenamePLY = "file_test.ply";
ifstream fin(FilenamePLY.c_str());
for (int i = 0; i < NumberBorderPoint; i++){
fin.read(&numFloat, sizeof(float));
for (int j = 0; j < 3; j++) {
fin.read(&numFloat, sizeof(float));
}
}
cout << fin.gcount() << endl;
for (int i = 0; i<BorderNumberTriangle; i++){
fin.read(&numInt, sizeof(int));
for (int j = 0; j<3; j++) {
fin.read(&numInt, sizeof(int));
}
}
cout << fin.gcount() << endl;
return 0;
}
The code compiled and executed under Windows outputs:
0
0
Whereas under Linux the output is:
4
4
My feeling is that the read function doesn't get the correct values to separate the numbers in the binary files but sizeof(float) and sizeof(int) both have the same value under Windows and Linux (4).
Any ideas of where the problem lies ?
Thank you for your help,
Try to open the file for binary reading:
ifstream fin(FilenamePLY.c_str(), std::ios::binary );
Open the file in binary mode and verify you opened the file.
You are also currently reading data of sizeof(float) into a character - its going to overwrite other data at that point (numFloat, numInt)
Here is my code:
#include <iostream>
int main(int argc, const char * argv[]) {
int size = 0;
int numbers[10000];
while (scanf("%i", &numbers[size]) != EOF) {
size++;
}
//some other functions...
return 0;
}
It is to solve a problem for competitive programming, and when entering the input(more than 1000 lines) to debug, it just stays there and never finishes. But if I enter just a part of the same input(more less 300 lines) it works propperly. Any ideas? I'm using Xcode.
scanf returns the number variables it was able to fill - however, this can even be 0 (zero) - if you enter some characters scanf cannot parse. If this happens, though, scanf will try to re-read one and the same input all the time, resulting in an endless loop.
Try this for illustration:
for(int i = 0; i < 10; ++i)
{
int n;
int r = scanf("%d", &n);
printf("%d / %d\n", r, n);
fflush(stdout);
}
and give input some numbers, then a non-decimal (e. g. 7 8 x).
So prefer while (scanf("%i", &numbers[size]) == 1) instead.
I am writing a very easy program for the open.kattis programming website. This is one of the easiest problems on their website so its quite a hit to my ego. When I test the code myself it works fine, but their results indicate that I get a runtime error on an unknown test case. The link to the problem description is: https://open.kattis.com/problems/everywhere but the general basis of the problem is I'm trying to determine the number of unique instances in a list of strings
My code is:
#include <iostream>
#include <string.h>
using namespace std;
int main()
{
short t; // test cases
short trips;
char city[21];
char cities[50][21];
bool found;
short count;
// read in the number of test cases
cin >> t;
// loop through each test case
for(int i=0; i<t; i++)
{
// read in the number of trips taken
cin >> trips;
// reset the count to 0
count = 0;
// loop through each trip
for(int j=0; j<trips; j++)
{
// read in the city
cin >> city;
// Linear search to determine if city has been visited
found = false;
for(int k=0; k<count; k++)
{
if(strcmp(city, cities[k]) == 0)
found = true;
}
// If city hasn't been visted, increment count and add to list
if(!found)
{
strcpy(cities[count], city);
count++;
}
}
// Output results for test case
cout << count << endl;
}
return 0;
}
You misread the description. char cities[50][21] isn't enough for this exercise:
The number of trips is at most 100 and no city name contains more than 20 characters.
Calling the number of possible cities "trips" is a little bit misleading here, but it's not the number of tests (T ≤ 50). That being said, you could improve your program a lot if you separate the concerns and actually use the C++ standard library:
#include <iostream>
#include <set> // <- Hint: those both will help you tremendously!
#include <string> // <-
int single_test_case(){
// ...
}
int main(){
int tests;
std::cin >> tests;
for(int i = 0; i < tests; ++i){
std::cout << single_test_case();
}
return 0;
}
I am trying to write a sudoku solver.
I got the input almost done, but something strange started happening. On the index [i][9] of int sudoku[i][9], there are numbers present that I have never put there.
For example, when I run the code below with the input that is commented below using namespace std;, the output is:
410270805
085146097
070580040
927451386
538697412
164328759
852704900
090802574
740965028
Of course, I only need 0 through 8, but I was wondering what is causing integers to appear at the 9th index.
This is the code:
#include <iostream>
#include <math.h>
#include <cstdlib>
using namespace std;
/*
410270805
085146097
070580040
927451386
538697412
164328759
852704900
090802574
740965028
*/
int main()
{
int sudoku[9][9];
int solving[9][9][9];
int input;
for (int i=0; i<=8; i++) {
cin >> input;
int j;
int k;
for (j=8, k=1; j>=0; j--, k++) {
int asdf = input/pow(10,k-1);
sudoku[i][j] = asdf % 10;
}
}
cout << endl;
for (int i=0; i<=8; i++) {
for (int j=0; j<=9; j++) {
cout << sudoku[i][j];
}
cout << endl;
}
return 0;
}
Accessing elements outside of the defined region of an array is Undefined Behavior (UB).
That means it could:
Allow you to access uninitialized space (what yours is doing hence the random numbers)
Segfault
Any number of other random things.
Basically don't do it.
In fact stop yourself from being able to do it. Replace those arrays with std::vectors and use the .at() call.
for example:
std::vector<std::vector<int>> sudoku(9, std::vector<int>(9, 0));
for (int i=0; i<=8; i++) {
for (int j=0; j<=9; j++) {
cout << sudoku.at(i).at(j);
}
cout << endl;
}
Then you will get a thrown runtime exception that explains your problem instead of random integers or segfaults.
I think I found your problem, at your very last for loop you used j <= 9 instead of j <= 8. You then tried to write (j) leaving the possibility of it writing 9 wide open. Try replacing that 9 with 8.