concatenating arrays, array 1 memory gone C++ - c++

Program consists of array1[20] and array2[40] from input files. Array1 is then added on to the end of array2 (after which they will be sorted chronologically, but I havent gotten there yet bc I am stuck on this). The final result will be printed; I did not include this portion of the code nor the code for inputing files to make it easier for you to read :) thanks for the help.
The code yeilds no errors. The input is number 1 through 20 on both input files and the output reads
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 135062784 135305664 134511120 135308884 6 0 6 16 135303844 134511520 134511136 134957510 135308884 135111932 135120188 0 134511156 135303952 134511168 134805750
int readSortedArray (int array[20], int count, istream& infile)
{
count = 0;
while( (count < 20) && (infile >> array[count]) )
{
count++;
}
return count;
}
int main ()
{
int array[20], array1[20], array2[40];
int count, count1, count2, x;
count1 = readSortedArray(array1, count1, infile1);
count2 = readSortedArray(array2, count2, infile2);
for (int i=1; i < count1 + 1; i++ )
{
array1[i - 1] >> array2[count2 + i];
}
return 0;
}

Seems the for loop in the main function should be this (i should start at 0). And make sure infile2 really contains what you think it contains.
for (int i=0; i < count1; i++ )
{
array2[count2 + i] = array1[i];
}

Related

i want to print no in 2d array by taking inputs but it will give wrong output

i am using vectors and i want to print same output as per the input by using the exact method in the code
trying to using 2 d vectors
//the cause of the error is the while j loop part
i mark it in the code
#include <bits/stdc++.h>
using namespace std;
// vector<int> dynamicArray(int *n,int *q)
// {
// }
int main()
{
int n, size, a;
cin >> n >> size;
vector<vector<int> > q;
// vector<int>q;
vector<int> q1;
for (int i = 0; i < size; i++) {
int j = 3;
// here error occurs
while (j > 1) {
cin >> a;
q1.push_back(a);
j--;
}
q.push_back(q1);
}
for (int i = 0; i < q.size(); i++) {
for (int j = 0; j < 3; j++) {
cout << q[i][j];
}
cout << endl;
}
return 0;
}
// here are the inputs
2 5
1 0 5
1 1 7
1 0 3
2 1 0
2 1 1
expected output
1 0 5
1 1 7
1 0 3
2 1 0
2 1 1
//here are the output
resulting output
100
105
105
105
105
I think that your code has 2 bugs in it. Firstly, you are not clearing vector q1 after every processed line, thus your code is pushing to q vector with the same prefix of values every time and that's why the output is the same line repeated multiple times.
Secondly, i think that you are trying to read 3 elements in every line but currently because of condition while(j > 1) you are reading only 2 elements. Try to change it to while(j > 0).

C++ matrix - Get number of columns where the lowest value is in the same row as the highest value in the columns after it

I'm writing a program in C++ where the inputs are N (the number of villages/rows), M (the number of days/columns) and an H[N][M]
matrix where I individually input the temperatures (min -50, max 50).
The output should be the total number of days when the village with the lowest
temperature has the highest forecast temperature, and after that the number (column) of these days in ascending order.
So if I input something like this:
3 5
10 15 12 10 10
11 11 11 11 20
12 16 16 16 20
The output should be:
2 2 3
Or input:
3 3
1 2 3
1 2 3
1 2 3
Output:
2 1 2
My approach was to first store the minimum temperatures and maximum forecast temperatures of each day into two separate arrays and
then writing a for loop where I check each village day by day if they contain both the minimum value on the given day and maximum forecast temperatures from that day on.
I have the following code:
#include <iostream>
const int maxarr = 1000;
int H[maxarr][maxarr];
using namespace std;
void read(int N, int M, int t[maxarr][maxarr]);
void count(int N, int M, int t[maxarr][maxarr]);
int main()
{
int N;
int M;
cout<<"Number of villages? ";
cin>>N;
cout<<"Number of days? ";
cin>>M;
read(N,M,H);
count(N,M,H);
return 0;
}
void read(int N, int M, int t[maxarr][maxarr])
{
for(int i = 0; i < N ; i++)
{
for(int j = 0; j < M ; j++)
{
cin>>t[i][j];
}
}
}
void count(int N, int M, int t[maxarr][maxarr])
{
int mintemparr[maxarr];
int maxtemparr[maxarr];
int mintemp;
int maxtemp;
int days[maxarr];
int cnt = 0;
for(int j = 0; j<M; j++)
{
mintemp = 51;
for(int i = 0; i<N; i++)
{
if(t[i][j]<mintemp)
{
mintemp = t[i][j];
}
mintemparr[j] = mintemp;
}
}
for(int i = 0; i < M-1; i++)
{
maxtemp = -51;
for(int j = 0; j < N; j++)
{
for(int k = i+1; k < M; k++)
{
if(t[j][k]>maxtemp)
{
maxtemp = t[j][k];
}
}
maxtemparr[i] = maxtemp;
}
}
for(int i = 0; i < M-1; i++)
{
for(int j = 0; j < N; j++)
{
for(int k = i+1; k < M; k++)
{
if(t[j][i] == mintemparr[i])
{
if(t[j][k] == maxtemparr[i])
{
days[cnt] = i+1;
cnt++;
//tried an i++ here, didn't work as intended
}
}
else
{
j++;
}
}
}
}
cout<<cnt<<" ";
for(int i = 0; i < cnt; i++)
{
cout<<days[i]<<" ";
}
}
There are some instances where it works perfectly, for example with the first input it's output is as it should be. But with the
second input I get
6 1 1 1 2 2 2
and a longer (1000x1000) input, which I obviously can't copy here also gives wrong results.
How could I make this code work as intended?
The reason why you're getting 6 1 1 1 2 2 2 for the second example is that you're not stopping to check whether a particular day fulfils the condition once you have found that it does. Thus you find that on day 1 the condition is fulfilled for village 1, village 2 and village 3 (the first three 1s in the result), and then the same happens for day 2.
From the comment
tried an i++ here, didn't work as intended
I guess you already identified that problem and the i++ was intended to prevent rechecking the same day again. However, as you noticed, that alone doesn't work - the reason here is that when skipping ahead to the next day you need to ensure that for that day checking the condition again starts with village 1 and the search for the highest temperature needs to begin from the start as well.
To do so, just add
++i; // carry on with the next day
j = 0; // start with the first village in the next iteration
k = i; // search for the highest temperature beginning from day i + 1
// note that at the end of the loop body `k` will be incremented
// so we need to use `k = i` instead of `k = i + 1` as in the loop-initializer here.
after cnt++ in place of the comment I quoted above.
With this change one gets the output you described in the question for both cases, as you can see here.
Given the input you uploaded to zippyshare I believe that the output for the second example should indeed be 3 1 2 3 instead of 2 1 2. Luckily the code is easy to change to accomodate that: Just replace all k = i + 1s by k = i and change the newly added k = i to k = i - 1 so that searching for the highest forecasts includes the present day.

How can I fix error when adding elements to my array

I am trying to add elements to my file using a method I have already used and was proven to be successful, however now when I do it I get the numbers I want as well as a bunch of other numbers that aren't in my file and don't make any sense
const int MAX_SIZE = 21;
int readSquare(int square[MAX_SIZE][MAX_SIZE], string inputFileName){ //reads file into an array
int value;
ifstream inFile;
inFile.open(inputFileName);
if (inFile) //if the input file to be read open successfully then goes on
{
int temp;
inFile >> temp;
if (temp>21) {
temp=21;
}
for (int i = 0; i < MAX_SIZE; i++)
{
for(int j = 0; j < MAX_SIZE; j++)
{
inFile >> square[i][j];
}
}
} else {
inFile.close();
return 0; //returns 0 if couldnt open file
}
inFile.close();
cout << "Magic square" << endl;
for(int i=0;i<MAX_SIZE;i++)
{
for(int j=0;j<MAX_SIZE;j++)
{
cout << square[i][j] << " ";
}
cout<<endl;
}
return 1;
}
This is the file I am using on my code
3
4 9 2
3 5 7
8 1 6
And this is the result I get(goes on for a while but I only took the top portion)
4 9 2 3 5 7 8 1 6 16840768 6619136 6643024 23198772 0 1942212500 127 917504 6643024 786434 6643032 0
65536 30 0 31 0 13930549 30 593 6619744 6619744 -2 127 46 6420808 1997546816 -1759127226 -2 6420704 1997359545 4096 4104
0 6420680 6634144 6619136 6421232 4104 6619744 0 3 0 4096 6420732 1997535944 6420804 655612 655360 2 9 0 2 6420976
0 1997378284 6420976 663276 1952 229640288 663200 655360 0 1997377793 6421060 661336 9 16777596 0 13080 236 661336 2 16777596 -530786634
Hat tip to #melpomene for working through the details in the main comments.
Op, you're iterating over the entire range of the array regardless of the availability of input data. I suggest you do the following so the results are less random in appearance:
initialize the values in the 2D array to zero.
limit the input samples to the quantity you're expecting, and not to exceed the size of the array in either dimension.
In your post, you're showing the value of 3 in the first line of the input file. What does that mean -- 3 lines, 3 samples, or 3 samples for each of the 3 lines?
Since the input file has 3 samples per line, I'm guessing the initial value in the data file represents the samples per line where the values for each line are assigned to an individual inner array.
Without deviating too much from your post, consider the following:
// clear the array for easier diags
for (int n = 0; n < MAX_SIZE; n++)
for (int m = 0; m < MAX_SIZE; m++)
square[n][m] = 0;
int cols;
inFile >> cols; // first line of data file indicating the samples in each row
if (cols > MAX_SIZE) // don't exceed the size of the inner array
cols = MAX_SIZE;
for (int i = 0; i < MAX_SIZE; i++)
{
for(int j = 0; j < cols; j++)
{
if (!(inFile >> square[i][j])) // read until EOF
{
i = MAX_SIZE; // force outer loop to terminate since break only affects the inner loop.
break;
}
}
}
See How does ifstream's eof() work?

draw ascii square spiral c++ with a filling in it (Output on terminal itself)

I have been desperately looking for a definite answer for this. The challenge was beyond my thinking scope or wasn't enough.
There was something topic like Spiral, but mostly is about matrix. If it is about the ascii art, nothing answer look's like this problem I have.
Suppose you have an integer N
int N;
Now, I input the N with an integer value
scanf("%d", N); fflush(stdin);
Let's see some examples:
1
#
5
#####
....#
###.#
#...#
#####
9
#########
........#
#######.#
#.....#.#
#.###.#.#
#.#...#.#
#.#####.#
#.......#
#########
11
###########
..........#
#########.#
#.......#.#
#.#####.#.#
#.#...#.#.#
#.#.###.#.#
#.#.....#.#
#.#######.#
#.........#
###########
and so on.
I have a difficulty to understand and write the algorithm for it.
I had tried to search for help but none of them fit my expectation.
Here are some of the last attempts
Attempt A
#include<stdio.h>
#include<iostream>
#include<math.h>
#include<string.h>
#include<ctype.h>
#include<stdlib.h>
using namespace std;
void Spirality(int N, char Border, char Fill) {
for (int i = 0; i < N; i++) {
for (int j = 0; j < i; j++) {
printf("%c", Border);
for (int k = 0; k < j; k++) {
printf("%c", Fill);
}
};
printf("\n");
};
};
int main() {
int N;
char Out = '#', In = '.';
char A[101][101];
scanf("%d", &N); fflush(stdin);
int len = N, k = 1, p = 0, i;
Spirality(N, Out, In);
printf("\n");
getchar();
return 0;
}
Result: N is 5
5
#
##.
##.#..
##.#..#...
Attempt B
#include<stdio.h>
#include<iostream>
#include<math.h>
#include<string.h>
#include<ctype.h>
#include<stdlib.h>
using namespace std;
void Spirality(int N, char Border, char Fill) {
for (int i = 0; i < N; i++) {
for (int i = 0; i < N; i++) printf("%c", Border);
printf("\n");
for (int i = 0; i < N; i++) printf("%c", Fill);
};
};
int main() {
int N;
char Out = '#', In = '.';
char A[101][101];
scanf("%d", &N); fflush(stdin);
Spirality(N, Out, In);
printf("\n");
getchar();
return 0;
}
Result: N is 5
#####
.....#####
.....#####
.....#####
.....#####
.....
None of these output expected spiral.
At first, let's take a look at what your program has to output (line by line).
When N = 11, the output should be like (next to the "image" I will write configuration of your signs):
########### 11
..........# 10 1
#########.# 9 1 1
#.......#.# 1 7 1 1 1
#.#####.#.# 1 1 5 1 1 1 1
#.#...#.#.# 1 1 1 3 1 1 1 1 1
#.#.###.#.# 1 1 1 1 3 1 1 1 1
#.#.....#.# 1 1 1 5 1 1 1
#.#######.# 1 1 7 1 1
#.........# 1 9 1
########### 11
The line 1 7 1 1 1 means that at first one sign (#) has to be written, then 7 signs (.), and then 3x one sign (.#.).
Because all of our lines starts and ends with ones, and there is only one number, different from one in each line, we can simplify our problem (output) like this:
11
10 1
9 2
1 7 3
2 5 4
3 3 5
4 3 4
3 5 3
2 7 2
1 9 1
11
Note that in each line, except 2nd one, the first character is '#'.
So the function for the one line of output can look like that:
const char sign[2] = { '#', '.' };
void outputline(int l, int m, int r, int currsign) {
int i;
for (i = 0; i < l; i++) {
cout << sign[currsign] << ' ';
currsign = (currsign + 1) % 2;
}
for (i = 0; i < m; i++) cout << sign[currsign] << ' ';
currsign = (currsign + 1) % 2;
for (i = 0; i < r; i++) {
cout << sign[currsign]<<' ';
currsign = (currsign + 1) % 2;
}
cout << endl;
}
In the function, the first argument tells us, with how many ones line begins, m is the only number in the line that is not 1, r tells us with how many ones line ends and currsign is the index of the character that the line begins with.
Now the problem is reduced on how 3 numbers changes from line to line. If I copy paste it from above:
l m r
0 11 0
0 10 1
0 9 2
1 7 3
2 5 4
3 3 5
4 3 4
3 5 3
2 7 2
1 9 1
0 11 0
We can see, that the first 2 lines are a bit special (and therefore can be written separately). But for all other lines we can see simple rule.
Until m is not 3, it decreases for 2 in every steps, while l and r increase for 1.
In the next step, m is 3 again, while l increases for 1 and r decreases for 1
And after that, until m is not N (11 in our case), m increases for 2 in every step, while l and r decrease for 1.
This algorithm works for every odd number.
You can do the same thing to see what are the patterns for each N(length of the side) divisible by 4 and for each N of the form 4*k+2.
So the main function should look like:
int main() {
int N, n;
cin >> N;
int left = 0, middle = N, right = 2;
if (N % 4 == 0) n = 4;
else n = 2;
outputline(0, middle--, 0, 0);
if (middle == 0) return 0;
outputline(0, middle--, 1, 1);
if (middle == 0) return 0;
for(middle; middle >=n; middle -=2)
outputline(left++, middle, right++, 0);
if (N % 2 == 1) {
middle += 2; right -= 2;
}
else if (N % 4 == 2) {
left--; middle += 4; right-=3;
}
else if (N % 4 == 0) {
left++; right--;
}
for (middle; middle <= N; middle += 2)
outputline(left--, middle, right--, 0);
return 0;
}
Hope that the answer helps you.

Finding the number of occurences of an integer in multidimensional array in C++

I wish to find the number of occurrences of a number taken as input in the given multidimensional array defined by the logic below:
...
int n,x,count=0;
cin>> n >> x;
int a[n][n] ;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
a[i][j]= i*j ;
}
}
for( int i=1;i<=n;i++)
{
for( int j=1;j<=n;j++)
{
if(a[i][j] == x)
++count;
}
}
cout<< count ;
...
For eg., if I give input as 6(n) and 12(to find its number of occurrences, x here). The multidimensional array looks something like this:
1 2 3 4 5 6
2 4 6 8 10 12
3 6 9 12 15 18
4 8 12 16 20 24
5 10 15 20 25 30
6 12 18 24 30 36
Now, the number of occurences of 12 here is 4(count).
But when I give n as 10 and x as 5, the program stops working. I can't seem to find what is happening. Can someone help me on this?
Also in what way can I modify my code?
How can I handle the case when n is as large as 1000 or 10k without changing the logic of the program?
Indices in C/C++ starts at 0. If an array is declared to have size n as in int a[n] the only valid indices are: 0,1,...,n-1 i.e. [0,n[
If you go out of bound undefined behaviour is expected. That should be your case.
Fix the loops as follows (note the new bounds and the +1 in i and j)
int a[n][n];
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
a[i][j]= (i+1)*(j+1) ;
#include <algorithm>
// ...
for(int i = 0; i < n; ++i) {
count += std::count(a[i], a[i] + n, x);
}
or a more simpler version:
std::cout << std::count(a[0], a[0] + n*n, x);