i am doing read file in c++ and this is my code :
#include <iostream>
#include <fstream>
#include <algorithm>
#include <climits>
using namespace std;
int main()
{
int row=0;
int col=0;
ifstream inputFile;
int arr[16][5];
inputFile.open("hdtt4req.txt");
if(inputFile.is_open()) {
inputFile >> arr[row][col];
for (row = 0; row < 16; row++) {
for (col = 0; col < 5; col++) {
cout <<"hi"; //arr[row][col];
cout << endl;
}
}
}
return 0;
}
and this is the file that i want to read:
1 2 2 1 2
2 1 1 1 2
3 1 1 1 6
4 2 2 3 2
1 2 5 1 2
2 0 4 3 2
3 1 2 1 0
4 2 2 1 2
1 2 1 1 2
2 0 0 5 1
3 2 1 4 1
4 6 1 2 1
1 3 1 2 1
2 1 4 1 4
3 3 3 2 1
4 2 0 1 1
after i compile , i get this kind of result. can anyone tell me what is the error ? thanks
row and col start start undefined, so the statement inputFile >> arr[row][col]; will give you undefined behavior. Make sure you set these values to zero before performing any operations
row = col = 0;
Related
What is wrong in my code why it is not giving correct output??
input
84
1 0 1 2 1 1 0 0 1 2 1 2 1 2 1 0 0 1 1 2 2 0 0 2 2 2 1 1 1 2 0 0 0 2 0 1 1 1 1 0 0 0 2 2 1 2 2 2 0 2 1 1 2 2 0 2 2 1 1 0 0 2 0 2 2 1 0 1 2 0 0 0 0 2 0 2 2 0 2 1 0 0 2 2
Its Correct output is:
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
And Your Code's output is:
0-36092119132636100007056629140-858993460214748364-...
#include<iostream>
#include<algorithm>
using namespace std;
void sortArray(int *arr,int n){
int low=0,mid=1,high=n-1;
while(mid<=high){
if(arr[mid]==1){
mid++;
}
else if(arr[mid]==2){
swap(arr[mid],arr[high]);
high--;
}
else{
swap(arr[mid],arr[low]);
mid++,low++;
}
}
for(int i=0;i<n;i++){
cout<<arr[i];
}
}
int main()
{
int t;
cin>>t;
while(t--){
int n;
cin>>n;
int arr[n];
for(int i=0;i<n;i++){
cin>>arr[n];
}
sortArray(arr,n);
}
return 0;
}
The main problem is in your input reading:
for(int i=0;i<n;i++) {
cin>>arr[n];
}
You are reading into arr[n] which is undefined. You want to use i as index:
for(int i=0;i<n;i++) {
cin>>arr[i];
}
Since the array is going to contain only 0, 1, or 2, you can simplify the sorting algorithm, too:
void sortArray(int *arr, size_t n)
{
size_t count[3] = {0};
for (size_t i = 0; i < n; ++i) {
count[arr[i]]++;
}
size_t k = 0;
for (size_t i = 0; i < 3; ++i) {
for (size_t j = 0; j < count[i]; ++j)
arr[k++] = i;
}
for (size_t i = 0; i < n; ++i)
std::cout << arr[i] << ' ';
std::cout << endl;
}
Note: you are using a non-standard extension. C++ standard doesn't have VLA (variable length arrays).
Variable length arrays are typically allocated on "stack" and is prone to stack overflow. If length of the array is too big, you will have undefined behaviour. Worse, you can't easily know the "right" size for the array, either. For that reason, VLAs are best avoided. You could use std::vector<int> instead.
you should try a better approach(Textbook approach) i.e to count how many times 0,1 and 2 are occurring and then assigning them in ascending order or please explain what approach you are using in your code.
void sort012(int a[], int n)
{
int count[3]={};
for(int i=0;i<n;i++){
count[a[i]]++;
}
int j=0;
for(int i=0;i<3;i++){
int temp=count[i];
while(temp--){
a[j]=i;
j++;
}
}
}
its an easy and efficient approach in terms of time and space complexity
So for my current homework assignment, I have to determine whether the sum of all the rows in a square are equal, if they are all equal then the square is 'Awesome' otherwise 'Not Awesome'. In this program the first input is the number of squares you want to solve and then for each square you enter how many rows it will have, followed by a series of integers to represent each number in the 'square'. Below is my tried and tested code, for me, I cant seem to come up with a test case that breaks it, yet when I upload it for grading, the grading bot fails it. Any help is greatly appreciated.
At first I thought it was an overflow issue, so I changed the sum and temp variables to long long but it had no effect.
input examples:
3
4
1 2 3 4
1 2 3 4
1 2 3 4
1 2 3 4
3
1 2 3
2 3 1
3 2 7
5
1 2 3 4 5
1 1 1 1 11
2 2 2 2 7
3 3 3 3 3
4 4 4 4 -1
output would be:
Awesome
Not Awesome
Awesome
#include <iostream>
using namespace std;
int main()
{
int numTests, numRows, i, col, row, firstRun;
long long sum, prevSum, temp;
cin >> numTests;
for (i = 0; i < numTests; i++)
{
firstRun = 1;
cin >> numRows;
for (row = 0; row < numRows; row++)
{
sum = 0;
for (col = 0; col < numRows; col++)
{
cin >> temp;
sum += temp;
}
if (!firstRun && prevSum != sum)
break;
firstRun = 0;
prevSum = sum;
}
if (row != numRows)
cout << "Not ";
cout << "Awesome" << endl;
}
return 0;
}
You are breaking the loop when you get the answer . But input may not finish .
1
4
1 2 3 4
1 2 3 1
1 2 3 4
1 2 3 4
Here , your loop will break after inputting second row . Thus the subsequent numbers are treating wrongly and causing WA . Consider the case
2
3
1 2 3
1 2 1
3 2 1
3
3 1 1
1 1 3
1 3 1
Your program will print
Not Awesome
Not Awesome
I'm trying to run this simple program and wondering why the output is coming wrong. The code queries for hardware concurrency, then tries to launch that amount of threads and do some task. To stub that task, I'm writing to individual elements of already resized vector, but the result is still coming wrong -
#include <iostream>
#include <thread>
#include <vector>
#include <functional>
void myMethod(std::vector<int> &v, int threadNumber) {
for(int i = threadNumber - 1; i < v.size(); i+= threadNumber) {
v[i] = threadNumber;
}
}
int main() {
const auto numThread = std::thread::hardware_concurrency();
std::vector<int> vec;
vec.resize(100, 0);
if(numThread < 2) {
std::cout << "Not running\n";
return 0;
}
std::vector<std::thread> vT;
for(int i = 1; i <= numThread; ++i) {
vT.emplace_back(myMethod, std::ref(vec), i);
}
for(auto &t: vT) { t.join(); }
for(const auto &i: vec) {
std::cout << i << ' ';
}
std::cout << std::endl;
}
Output comes as -
1 2 3 4 1 3 1 4 3 2 1 4 1 2 3 4 1 3 1 4 3 2 1 4 1 2 3 4 1 3 1 4 3 2 1 4 1 2 3 4 1 3 1 4 3 2 1 4 1 2 3 4 1 3 1 4 3 2 1 4 1 2 3 4 1 3 1 4 3 2 1 4 1 2 3 4 1 3 1 4 3 2 1 4 1 2 3 4 1 3 1 4 3 2 1 4 1 2 3 4
but I was expecting - 1 2 3 4 1 2 3 4 ...
In your myMethod function, you're increasing variable i with wrong amount -
void myMethod(std::vector<int> &v, int threadNumber) {
for(int i = threadNumber - 1; i < v.size(); i+= threadNumber) {
v[i] = threadNumber;
}
}
should actually be -
void myMethod(std::vector<int> &v, int threadNumber) {
const auto numThread = std::thread::hardware_concurrency();
for(int i = threadNumber - 1; i < v.size(); i+= numThread) {
v[i] = threadNumber;
}
}
I am trying to generate a list of subsets from a set. For example, if I had n = 6, and r = 4, I would have 15 possible combinations which would be the following:
0 1 2 3
0 1 2 4
0 1 2 5
0 1 3 4
0 1 3 5
0 1 4 5
0 2 3 4
0 2 3 5
0 2 4 5
0 3 4 5
1 2 3 4
1 2 3 5
1 2 4 5
1 3 4 5
2 3 4 5
My current code does work with the above subsets if n = 6 & r = 4. It also works if any other combination of n-r=2. It does not work for anything else and I'm having a bit of trouble debugging since my code makes perfect sense to me. The code I have is the following:
int array[r];
int difference = n-r;
for(int i = 0; i < r; i++){
array[i] = i;
}
while (array[0] < difference){
print (array, r);
for(int i = r-1; i >= 0; i--){
if ((array[i] - i) == 0){
array[i] = array[i] + 1;
for (int j = i+1; j < r; j++){
array[j] = j + 1;
}
i = r;
}
else{
array[i] = array[i] + 1;
}
print (array, r);
}
}
}
To give some context, when I plug in n=6 and r=3, I am supposed to have 20 combinations as the output. Only 14 are printed, however:
0 1 2
0 1 3
0 1 4
0 2 3
0 2 4
0 3 4
1 2 3
1 2 4
1 3 4
2 3 4
2 3 4
2 3 5
2 4 5
3 4 5
It does print the first and last output correctly, however I need to have all the outputs printed out and correct. I can see after the 3rd iteration, the code starts failing as it goes from 0 1 4 to 0 2 3 when it should go to 0 1 5 instead. Any suggestions as to what I'm doing wrong?
Here's what I think you are trying to do. As far as I can tell, your main problem is that the main for loop should start over after incrementing an array element to a valid value, rather than continuing.
So this version only calls print in one place and uses break to get out of the main for loop. It also counts the combinations found.
#include <iostream>
void print(int array[], int r) {
for(int i=0; i<r; ++i) {
std::cout << array[i] << ' ';
}
std::cout << '\n';
}
int main() {
static const int n = 6;
static const int r = 3;
static const int difference = n-r;
int array[r];
for(int i = 0; i < r; i++) {
array[i] = i;
}
int count = 0;
while(array[0] <= difference) {
++count;
print(array, r);
for(int i=r-1; i>=0; --i) {
++array[i];
if(array[i] <= difference + i) {
for(int j=i+1; j<r; ++j) {
array[j] = array[j-1] + 1;
}
break;
} } }
std::cout << "count: " << count << '\n';
}
Outputs
0 1 2
0 1 3
0 1 4
0 1 5
0 2 3
0 2 4
0 2 5
0 3 4
0 3 5
0 4 5
1 2 3
1 2 4
1 2 5
1 3 4
1 3 5
1 4 5
2 3 4
2 3 5
2 4 5
3 4 5
count: 20
#include <iostream>
using namespace std;
int main() {
const int SIZE = 5;
double x[SIZE];
for(int i = 2; i <= SIZE; i++) {
x[i] = 0.0;
cout << i << endl;
}
}
Output:
3
4
0
1
2
3
4
0
1
2
3
4
0
1
2
3
4
0
1
2
3
4
0
1
2
3
4
0
1
2
3
4
0
1
2
3
4
0
1
2
3
4
0
1
2
3
4
0
1
2
3
4
0
1
2
3
4
0
1
2
3
4
0
1
2
3
4
0
1
2
3
4
0
1
2
3
4
0
1
2
3
4
0
1
2
3
4
0
1
2
3
4
0
1
2
3
4
0
1
2
3
4
0
1
2
3
4
0
1
2
3
4
0
1
2
3
4
0
1
2
3
4
0
1
2
3
4
...
If SIZE is initialized to a different value, the iterator will iterate until it is one short of that value and then reset back to zero. If the array of x is changed to data type int, the loop does not get stuck on itself. If the assignment value to x[i] is changed to any non-zero number, the value of is changed to garbage during the last run of the loop.
#include <iostream>
using namespace std;
int main() {
const int SIZE = 5;
double x[SIZE];
for(int i = 2; i <= SIZE; i++) {
x[i] = 1;
cout << i << endl;
}
}
Output:
2
3
4
1072693248
#include <iostream>
using namespace std;
int main() {
const int SIZE = 5;
int x[SIZE];
for(int i = 2; i <= SIZE; i++) {
x[i] = 1;
cout << i << endl;
}
}
Output:
2
3
4
5
You are writing past the end of the x array. x[] ranges from 0 to SIZE - 1 (or 4), and you let your index i == SIZE.
So, the behavior is undefined and coincidentally, you are overwriting i when you write x[5].
Use a debugger. It's your friend.
for(int i = 2; i < SIZE; i++) // i <= SIZE will write beyond the array
Your current array is of size 5. Arrays are 0 indexed:
1st element last element
0 1 2 3 4
You're iterating past the end of your array (i <= 5), which is undefined behavior.
Your end condition is wrong. Use i < SIZE
#include <iostream>
using namespace std;
int main() {
const int SIZE = 5;
double x[SIZE];
for(int i = 2; i < SIZE; i++) {
x[i] = 0.0;
cout << i << endl;
}
}