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!!
Related
I'm having trouble with a C6385 warning in my code. I'm trying to see if two arrays will equal each other. The warning I keep getting is on the line where if(p[i] == inputGuess[j]). I have tried redoing these line but I keep getting the same warning. Does anyone know what I'm doing wrong. This is also my first time programming in C++.
#include <time.h>
#include <stdlib.h>
#include <stdio.h>
#include "Game.h"
#include <iostream>
#include <iomanip>
using namespace std;
int* generateNumbers(int n, int m) {
// Intialize random number
srand(static_cast<unsigned int>(time(NULL)));
// Declare array size to generate random numbers based on what is between 1 to (m)
int* numbers = new int[n];
for (int i = 0; i < n; i++) {
numbers[i] = (rand() % m) +1;
cout << numbers[i]<< " " << endl;
}
return numbers;
}
void Game::guessingGame(int n, int m) {
int* p;
int sum = 0;
// Call the generateNumber function
generateNumbers(n, m);
// Declare array based on user guesses
inputGuess = new int[n];
p = generateNumbers(n,m);
for (int i = 0; i < n; i++) {
cin >> inputGuess[i];
}
// See if the user guesses and computers answers match up
for (int i = 0; i < n; i++) {
for (int j = 0; i < n; j++) {
if (p[i] == inputGuess[j]){ //Where I keep getting the C6385 Warning
sum++;
break;
}
}
}
}
The C6385 warning documentation states:
The readable extent of the buffer might be smaller than the index used
to read from it. Attempts to read data outside the valid range leads
to buffer overrun.
https://learn.microsoft.com/en-us/cpp/code-quality/c6385?view=msvc-160
Your second if statement compares i < n instead of j < n and since i is never modified inside it will run forever. This causes the warning since you’ll access memory out of bounds. Fix the comparison.
I am trying to build a code where I have to declare a large array in the heap.
At the same time I will use the boost library to perform some matrix calculations (as can be seen in Fill a symmetric matrix using an array
).
My limitations here are two : I will deal with large arrays and matrices so I have to declare everything on the heap and I have to work with arrays and not with vectors.
However I am facing a rather trivial for many people problem... When filling the matrix, the last element doesn't get filled in correctly. So although I expect to get
[3,3]((0,1,3),(1,2,4),(3,4,5))
the output of the code is
[3,3]((0,1,3),(1,2,4),(3,4,2.6681e-315))
I am compiling this code in ROOT6. I don't think it's related to that, I am just mentioning it for completion.
A small sample of the code follows
#include <iterator>
#include <iostream>
#include <fstream>
#include </usr/include/boost/numeric/ublas/matrix.hpp>
#include </usr/include/boost/numeric/ublas/matrix_sparse.hpp>
#include </usr/include/boost/numeric/ublas/symmetric.hpp>
#include </usr/include/boost/numeric/ublas/io.hpp>
using namespace std;
int test_boost () {
using namespace boost::numeric::ublas;
symmetric_matrix<double, upper> m_sym1 (3, 3);
float* filler = new float[6];
for (int i = 0; i<6; ++i) filler[i] = i;
float const* in1 = filler;
for (size_t i = 0; i < m_sym1.size1(); ++ i)
for (size_t j = 0; j <= i && in1 != &filler[5]; ++ j)
m_sym1 (i, j) = *in1++;
delete[] filler;
std::cout << m_sym1 << std::endl;
return 0;
}
Any idea on how to solve that?
Arrays and pointers are not objects of class type, they don't have members. You already have a float *, it is filler.
float const* in1 = filler; // adding const is always allowed
I've manged to finally solve it by changing &filler[5] to &filler[6].
So a version that works is seen below
#include <iterator>
#include <iostream>
#include <fstream>
#include </usr/include/boost/numeric/ublas/matrix.hpp>
#include </usr/include/boost/numeric/ublas/matrix_sparse.hpp>
#include </usr/include/boost/numeric/ublas/symmetric.hpp>
#include </usr/include/boost/numeric/ublas/io.hpp>
using namespace std;
int test_boost () {
using namespace boost::numeric::ublas;
symmetric_matrix<double, upper> m_sym1 (3, 3);
float* filler = new float[6];
for (int i = 0; i<6; ++i) filler[i] = i;
float const* in1 = filler;
for (size_t i = 0; i < m_sym1.size1(); ++ i)
for (size_t j = 0; j <= i && in1 != &filler[6]; ++ j)
m_sym1 (i, j) = *in1++;
delete[] filler;
std::cout << m_sym1 << std::endl;
return 0;
}
Running this code yields the following output
[3,3]((0,1,3),(1,2,4),(3,4,5))
I am trying to learn arrays and I can not figure out this code. For some reason when I run this an error is identified, but the entire code is highlighted as the error so I am confused as to where the error really is. I know this is very basic, but any help would be very appreciated.
#include <iostream>
using namespace std;
int main()
{
int x[8];
for (int i =0; i<= 8; i++)
x[i] = i;
return 0;
}
Try
#include <iostream>
using namespace std;
int main()
{
int x[8];
for (int i =0; i<8; i++)
x[i] = i;
return 0;
}
Because arrays start at 0 accessing array index 8 is to far as that is actually slot 9.
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
This code is supposed to make a array of strings, randomly order them, and then print the order. Unfortunately it adds a blank line in one of the spaces ( i think this is getline's doing). Any ideas how to fix that? I tried setting array [0] = NULL; it complains about operators...
#include <iostream>
#include <stdio.h>
#include <conio.h>
#include <time.h>
#include <string>
#include <cstdlib>
using std::cout;
using std::endl;
using namespace std;
void swap (string &one, string &two)
{
string tmp = one;
one = two;
two = tmp;
}
int rand_loc (int size)
{
return (rand() % size);
}
int main()
{
srand(time(NULL));
int size;
cin >> size;
string *array = new string[size];
//array[0] = NULL ;
for (int x = 0; x < size; x++)
{
getline(cin, array[x]);
}
//for (int x = 0; x < size; x++)
//{
// swap (array[rand_loc(size)], array[rand_loc(size)]);
//}
cout << endl;
for (int x = 0; x < size; x++)
{
//out << array[x] << endl;
int y = x + 1;
cout<<y<<"."<<" "<<array[x]<<endl;
}
delete[] array;
}
The first call to getline() will immediately hit the newline that the user entered after inputting size, and will therefore return an empty string. Try to call cin.ignore(255, '\n'); before the first call to getline(). This will skip up to 255 (an arbitrarily selected number) characters until a \n is encountered (and the newline will be skipped as well).
Edit: As #Johnsyweb and #ildjarn point out, std::numeric_limits<streamsize>::max() is a much better choice than 255.