Hello i have a code like this:
#include <iostream>
#include <cstdio>
using namespace std;
int main () {
std::string s="fawwaz";
...
}
then i compiled it with G++ using the gnu gcc online installer i've downloaded from gcc.gnu.org, The compilation runs without any errors and warnings, but when i run, an error appears "program a.exe has stopped working".
and the program runs without any error. Then i try to compile the original file (without double backslash infront of string declaration) the program compiled and run succesfully.
Whats the solution? Where's the problem? Is they any way to fix my problem so i can compile my program via command line NOT via Microsoft Visual C++ since it would be faster to compile via command line? :D
Thank you
This is the complete code :
#include <cstdio>
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
using namespace std;
void Cetak_Puzzle_Start(){
}
int main(int argc, char const *argv[])
{
string s;
ifstream file("input.txt");
vector<vector<int> > Puzzle_Start;
vector<vector<int> > Puzzle_Finish;
int Puzzle_size=0;
/*
* RETRIEVE PUZZLE SIZE
**/
getline(file,s);
for (int i = 0; i < s.length(); ++i)
Puzzle_size= (Puzzle_size*10) + (int) (s[i]-'0');
/*
* Set Zero ukuran 3x3 vector Puzzle start dan Puzzle finish
**/
vector<int> vtemp(Puzzle_size,0);
for (int i = 0; i < Puzzle_size; ++i)
{
Puzzle_Start.push_back(vtemp);
Puzzle_Finish.push_back(vtemp);
}
/*
* RETRIEVE START STATE
**/
getline(file,s);
int m=0,n=0; // dummy var for looping only m:pointer baris, n:pointer kolom,
for (int i = 0; i < s.length(); ++i)
if (n<Puzzle_size){
if (s[i]=',')
n++;
else if (s[i] >= '0' && s[i] <='9')
Puzzle_Start[m][n]= (Puzzle_Start[m][n] * 10) +(int) (s[i]-'0');
else if (s[i] ='B')
Puzzle_Start[m][n]=-1;
}else{
n=0; // Ganti baris
m++;
}
fclose(stdin);
/*
* CETAK PUZZLE
**/
// for (int i = 0; i < Puzzle_Start.size(); ++i){
// for (int j = 0; j < Puzzle_Start[i].size(); ++j)
// printf("%d ",Puzzle_Start[i][j]);
// printf("\n");
// }
return 0;
}
Here's the bug,
if (s[i]=',')
should be
if (s[i]==',')
and
else if (s[i]='B')
should be
else if (s[i]=='B')
Confusing = (assignment) and == (equality) is a very common error to make
Related
I am trying to initialise two 2D arrays, namely psi_0 and omega_0, with the data type as double. The sizes of the arrays are 401x401. I have written the last printing statement to check if the code is running properly or not. As soon as I initialise arrays, the last line doesn't get printed and this is getting displayed only: PS C:\Users\Avii\Desktop> cd "c:\Users\Avii\Desktop\" ; if ($?) { g++ trialc++.cpp -o trialc++ } ; if ($?) { .\trialc++ }.
Following is my code:
#include<iostream>
#include<iomanip>
#include <math.h>
#include <vector>
#include <algorithm>
#include <thread>
#include <chrono>
using namespace std;
void display(vector<double> &v){
for (int i = 0; i < v.size(); i++)
{
cout<<v[i]<<" ";
}
cout<<endl;
}
int main(){
const int N = 401;
const double pi = 3.141592653589793;
double L = pi/2;
double r = 1.5;
//-------------------------------------------------------------------
.
..
...
double psi_0[N][N];
double omega_0[N][N];
for (int i = 0; i < N; i++)
{
for (int j = 0; j < N; j++)
{
psi_0[i][j] = sin(x_vec[i]) * (sin(y_vec[j]));
omega_0[i][j]= 2 * sin(x_vec[i]) * sin(y_vec[j]);
}
}
cout<< "everything is fine"; // to check if the code has run till last or not!
return 0;
}
Here x_vec and y_vec are two vectors of size 401 and data type as double. I have tried using flush but could not see the problem is solved!
Any help is highly appreciated!!
My code goes through the values of each sample in a JSON file and gives out the number of sorting comparisons, the amount of memory, and time it takes for the sorting to happen. But for some reason the code is throwing me an error. Would love it if someone could help me out.
I'm getting a [json.exception.type_error.302] for some reason in my code. I know what the error means but I don't know where there's a fault
To be exact, this is the error I'm getting - "libc++abi.dylib: terminating with uncaught exception of type nlohmann::detail::type_error: [json.exception.type_error.302] type must be number, but is null"
#include <iostream>
#include <fstream>
#include "json.hpp"
#include "mergesort.h"
#include "insertionsort.h"
#include "quicksort.h"
#include <ctime>
#include <iomanip>
int main(int argc, char* argv[]) {
std::ifstream file;
file.open(argv[1]);
nlohmann::json jsonObject;
if (file.is_open()) {
file >> jsonObject;
} else {
std::cout << "Invalid Argument" << std::endl;
}
clock_t mergeTime, quickTime, insertTime;
extern int insertMemAccesses, insertCompare, mergeMemAccesses, mergeCompare, quickCompare, quickMemAccesses;
// std::cout << jsonObject;
// nlohmann::json jsonOutput;
std::cout << "Sample,InsertSortTime,InsertionSortCompares,InsertionSortMemaccess,MergeSortCompares,MergeSortMemaccess,QuickSortTime,QuickSortCompares,QuickSortMemaccess";
int arraysize = jsonObject["metadata"]["arraySize"];
int numsamples = jsonObject["metadata"]["numSamples"];
for (int i = 0; i <= numsamples; i++) {
std::string sample;
if (i < 9) {
sample = "Sample0" + std::to_string(i+1);
} else {
sample = "Sample" + std::to_string(i+1);
}
std::vector<int> insertion;
std::vector<int> merge;
std::vector<int> quick;
for(int j = 0; j <= arraysize; j++){
insertion.push_back(jsonObject[sample][j]);
merge.push_back(jsonObject[sample][j]);
quick.push_back(jsonObject[sample][j]);
}
insertTime = clock();
InsertionSort(&insertion);
insertTime = clock() - insertTime;
insertTime = ((unsigned long)insertTime)/CLOCKS_PER_SEC;
mergeTime = clock();
MergeSort(&merge);
mergeTime = clock() - mergeTime;
mergeTime = ((unsigned long)mergeTime)/CLOCKS_PER_SEC;
quickTime = clock();
QuickSort(&quick);
quickTime = clock() - quickTime;
quickTime = ((unsigned long)quickTime)/CLOCKS_PER_SEC;
std::cout<<sample<<",";
printf("%.6lu,%d,%d,%.6lu,%d,%d,%.6lu,%d,%d\n",insertTime,insertCompare,insertMemAccesses,mergeTime,mergeCompare,mergeMemAccesses,quickTime,quickCompare,quickMemAccesses);
insertCompare = 0;
insertMemAccesses = 0;
mergeCompare = 0;
mergeMemAccesses = 0;
quickCompare = 0;
quickMemAccesses = 0;
}
return 0;
}
Step through the code, and which line does the exception happen on? If you don't have a way to step through, then add in some std::cerr calls throughout to see where it is failing. I would assume that the error occurs on one of the push_back lines, most likely the first. You have j <= arraysize, which most likely goes out of bounds, so when you try to access jsonObject[sample][arraysize] this causes the error.
Also, you could probably just do quick = merge = insertion = jsonObject[sample].get<std::vector<int>>(), instead of needing the loop
I have written a small program in c++ that will take as an input a string. It will then print the first non-repeated character in the string. Below is my code. This is for a challenge on CodeEval.com. The thing is, as far as I can tell, the code works as it's supposed to. But CodeEval.com tells me my code is not correct. Unfortunately, I'm not allowed to see the input they're using, but when used at home, I see no problem. Can anyone tell me if there's anything about my code that does not fit the prompt?
#include <cstdlib>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main(int argc, char *argv[])
{
string input = "";
while(getline(cin, input)){
vector<char> inputVector(input.begin(),input.end());
char firstNonRepeatedChar;
for(int i = 0; i < inputVector.size(); i++){
if((inputVector[i] != inputVector[i + 1]) && (inputVector[i] != inputVector[i - 1])){
firstNonRepeatedChar = inputVector[i];
break;
}
}
cout << firstNonRepeatedChar << "\n";
}
//system("PAUSE");
return EXIT_SUCCESS;
}
EDIT:
This is the code that gave me the right answer, if anyone is wondering. Ben helped me realize that I wasn't answering the question properly based on the prompt.
#include <cstdlib>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int findCharInVector(vector<char>& input, char charToFind);
int main(int argc, char *argv[])
{
string input = "";
while(getline(cin, input)){
vector<char> characters(input.begin(),input.end());
char firstNonRepeatedChar;
for(int i = 0; i < characters.size(); i++){
if(!(findCharInVector(characters, characters[i]) > 1)){
firstNonRepeatedChar = characters[i];
break;
}
}
cout << firstNonRepeatedChar << "\n";
}
//system("PAUSE");
return EXIT_SUCCESS;
}
int findCharInVector(vector<char>& input, char charToFind){
int output = 0;
for(int i = 0; i < input.size(); i++){
if(input[i] == charToFind){
output++;
}
}
return output;
}
Based on the link in the comments, a non-repeated character is one that only appears in the string once. Here is the example given:
yellow // y
tooth // h
Your code:
if((inputVector[i] != inputVector[i + 1]) && (inputVector[i] != inputVector[i - 1])){
firstNonRepeatedChar = inputVector[i];
Is only checking if a character is not repeated consecutively. If it was the way you are thinking, then the example above tooth, the first non-repeated would be t, not h as the example specifies.
Furthermore inputVector[i] != inputVector[i - 1] will cause undefined behavior for i == 0.
Currently I am working on a problem to reformat the inputted string into the odd char then even char with no newline. ex. Input: Good Test. Ouput: Go etodTs. For some reason when I run the program it only outputs a "G".
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <cstdlib>
using namespace std;
int main (int argc, char ** argv) {
char sWordOdd[100] = {0};
scanf("%s", sWordOdd);
int iNum = strlen(sWordOdd);
for (int i=0; i<=iNum && i%2==0; i++) {
printf("%c",sWordOdd[i]);
}
for (int a=0; a<=iNum && a%2!=0; a++) {
printf("%c",sWordOdd[a]);
}
printf("\n");
return 0;
}
Your i<=iNum && i%2==0 break condition terminates your loop early. To achieve the effect you want, put an if statement inside the loop, like so:
for (int i = 0; i <= iNum; i++){
if(i % 2 == 0)
printf("%c",sWordOdd[i]);
}
As for your second loop, I think you meant a++ instead of a--, because otherwise you'll try to access the array using a negative index. I think you meant for your loop to look like this:
for (int a = 0; a <= iNum; a++){
if(a % 2 != 0)
printf("%c",sWordOdd[a]);
}
Side note: Notice the spacing between the variables and the operators in my code. Using spaces like this makes your code easier to read.
int main()
{
string line;
getline(cin, line);
for (size_t start : {0,1})
for (size_t ii = start; ii < line.size(); ii += 2)
cout << line[ii];
cout << endl;
}
The above code handles arbitrarily long lines, is C++11 rather than C, and works.
As said before the loops terminate if i<=iNum && i%2==0 is true (for the first loop), which is the case for i=0. The second loop terminates with i=1.
Since you want to iterate all characters you have to move the i%2==0 part out of the for-statement:
for (int i=0; i<=iNum; i++) {
if (i%2==0)
{
printf("%c",sWordOdd[i]);
}
}
The second loop needs to be modified in the same way...
To fix this problem you have to use a function that can see stuff after whitespaces. This function is called getline. But with getline, you have to use string, so in this example I used string. I then found the size with the .size() function and then using just one constraint for the for loop instead of two in the question. I also took off the char array and replaced it with string as stated above. Everything else is pretty much the same. Using this answer lets me not having to use cstring and also simplifying my code into a short, easy way that is easy to follow through.
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#define Set(a, s) memset(a, s, sizeof (a))
#define Rd(r) freopen(r, "r", stdin)
#define Wt(w) freopen(w, "w", stdout)
using namespace std;
int main (int argc, char ** argv)
{
string sWordOdd;
getline(cin, sWordOdd, '\n');
int iNum = (int)sWordOdd.size();
for (int i=0; i<iNum; i+=2){
cout << sWordOdd[i];
}
for (int a=1; a<iNum; a+=2){
cout << sWordOdd[a];
}
printf("\n");
return 0;
}
I'm trying to solve 56th problem of Project Euler and I have some troubles while using the Big Integer Library. Here is the code :
#include <iostream>
#include "BigInteger.hh"
#include <cmath>
using namespace std;
int digitSum(BigInteger x){
int num[(int)floor(log10(x))+2];
int n = 0;
BigInteger sum = 0;
while (x != 0){
sum += x - floor(x/10) * 10;
x = floor(x/10);
n++;
}
return sum.toInt();
}
int main(int argc, const char * argv[])
{
int max = 0;
for (int i = 1; i <= 100; i++){
for (int j = 1; j <= 100; j++){
cout << "i = %i, j = %i ",i,j;
if (digitSum(pow(i,j)) < max)
max = digitSum(pow(i,j));
}
}
cout << digitSum(pow(2,63));
return 0;
}
The problem is that when I try building, the compiler gives error on the lines using the log10, floor, and pow functions saying that they are used but not defined. When I comment the line #include "BigInteger.hh", everything goes fine but this time, of course, I can't use the Big Integer library.
Why is there such a problem? How to solve it?
Is it a compiler or a linker problem? If latter, try adding -lm to the command line to link the math library. Assuming GNU gcc here.