### Need an help with an arrays

Hello, so I got a file :

3 5
4 7 3 5 8
8 9 7 8 6
5 4 6 7 6

n = 3 (how many rows there are)
k = 5 (how many columns there are)
Other numbers are an array.

G[] is my an array.

What's happening I have to add the sum of the near numbers and divide by how many I added them and then replace them in the array. So for example
4 7 3 5 8

4 + 7 = 11 / 2 = 5
4 + 7 + 3 = 14 / 3 = 4
3 + 7 + 5 = 15 / 3 = 5
5 + 3 + 8 = 16 / 3 = 5
8 + 5 = 13 / 2 = 6

The first row's column is now
5 4 5 5 6 ( Keep in mind that they all integers so that's why)

The algorithm probably is okay but I get random numbers that are probably "junk" or "trash" whatever you call them.

 ``12345678910111213141516171819202122232425262728293031323334`` ``````#include #include using namespace std; const char duom[] = "d.txt"; const char rez[] = "r.txt"; void adjustSignals(int n, int k, int G[]) { ifstream in(duom); ofstream out(rez); in >> n >> k; for(int i = 0; i < n; i++) { for(int j = 0; j < k; j++) in >> G[j]; for(int t = 0; t < k; t++) { if(G[t] == 0) G[t] = (G[t] + G[t + 1]) / 2; else if(G[t] == t - 1) G[t] = (G[t] + G[t - 1]) / 2; else G[t] = (G[t - 1] + G[t] + G[t + 1]) / 3; out << G[t] << " "; } out << endl; } } int main() { int G[100], n, k; adjustSignals(n, k, G); return 0; } ``````
try initialize g to zeros g[100] = {0}; will do it.
You may also want to move in by one (start the data inside g) so you never go out of bounds.
at a glance it looks like for(t = 0... ) ... else g[t-1] is at risk?

It's not initialization problem because it reads file fine.

"678501 226170 75392 25135 8381
678503 226173 75396 25136 8380
678500 226170 75394 25135 8380 "

What do you mean else is at risk ? If G[t] is not 0 and not the last number in the array t - 1 it's gonna take the number before him and the one the right side because the number is not in the corner.
DdavidDLT wrote:
It's not initialization problem

I would leave all possibilities open, since you say you're getting junk values. This either means initialization problem, or going out-of-bounds of the array.

Unless you're absolutely sure, perhaps add in a simple "`if (t == 0) { cout << "ERROR\n" };`" check inside the branches where you attempt to access G[t - 1].

Here's where I see a problem:
On line 16, you enter values for G[0] through G[k-1].
After this, t is iterated from t = 0 to t = k - 1, inclusive.
But then you attempt to access G[t + 1], which is equivalent to accessing G[k], which you never gave a proper value. That's why jonnin suggested initializing the entire array to 0.

On the other hand, initializing everything to 0 might just mask the problem if you're not supposed to access the unassigned values anyway. Perhaps you should do this:

 ``12345`` ``````int G[100]; for (int i = 0; i < 100; i++) { G[i] = -99999 + i; }``````

Give some predictable, but crazy outrageous values to the initial values of the array.
Last edited on
Why would I enter values on 16th line to k - 1 ? I would just lose my 5th element.

EDIT : Okay I tried just for the sake of curiosity change G[t] == 4; Why ? Because I believe that G[t] takes the element instead of going through the loop in which place is the element. And guess what? I predicted that the first element is going to be 5. I'm not sure if it's just lucky random number or it really works like that. Never mind there's no way it could set to element since t is a 0;

 ``12345678`` `````` for(int i = 0; i < n; i++) { for(int j = 0; j < k; j++) in >> G[j]; for(int t = 0; t < k; t++) { if(G[t] == 0) G[t] = (G[t] + G[t + 1]) / 2; else if(G[t] == k - 1) G[t] = (G[t] + G[t - 1]) / 2; else G[t] = (G[t - 1] + G[t] + G[t + 1]) / 3;``````
Last edited on
 ``12345678910111213141516171819202122232425262728293031323334353637383940414243`` ``````#include #include const int MAX_COLS = 100 ; void adjustRow( int ncols, int row[] ) { if( ncols < 2 ) return ; // don't try to be be too clever; just make a copy // (we need the old unmodified values for our calculations) int temp[MAX_COLS] ; std::copy( row, row+ncols, temp ) ; row[0] = ( temp[0] + temp[1] ) / 2 ; row[ncols-1] = ( temp[ncols-2] + temp[ncols-1] ) / 2 ; for( int i = 1 ; i < ncols-1 ; ++i ) row[i] = ( temp[i-1] + temp[i] + temp[i+1] ) / 3 ; } void adjustSignals( int nrows, int ncols, int G[][MAX_COLS] ) { for( int r = 0 ; r < nrows ; ++r ) adjustRow( ncols, G[r] ) ; } int main() { const int NROWS = 3 ; int ncols = 5 ; int a[NROWS][MAX_COLS] { { 4, 7, 3, 5, 8 }, { 8, 9, 7, 8, 6 }, { 5, 4, 6, 7, 6 } }; adjustSignals( NROWS, ncols, a ) ; for( int r = 0 ; r < NROWS ; ++r ) { for( int c = 0 ; c < ncols ; ++c ) std::cout << a[r][c] << ' ' ; std::cout << '\n' ; } }``````

http://coliru.stacked-crooked.com/a/dceb4282cf778e25
 ``12345678910111213141516171819202122232425262728293031323334353637383940`` ``````#include #include #include #include using namespace std; template vector moving_averages( const vector &A, int spread ) { int N = A.size(); vector result( N ); for ( int j = 0; j < N; j++ ) { int first = max( 0, j - spread ), last = min( N - 1, j + spread ); T sum = 0; for ( int m = first; m <= last; m++ ) sum += A[m]; result[j] = sum / ( last - first + 1 ); } return result; } template void printVec( const vector &A, int width ) { for ( auto e : A ) cout << setw( width ) << e << ' '; cout << '\n'; } int main() { vector< vector > A = { { 4, 7, 3, 5, 8 }, { 8, 9, 7, 8, 6 }, { 5, 4, 6, 7, 6 } }; cout << "Original matrix:\n"; for ( auto row : A ) printVec( row, 3 ); cout << "Moving averages:\n"; for ( auto row : A ) printVec( moving_averages( row, 1 ), 3 ); }``````

 ```Original matrix: 4 7 3 5 8 8 9 7 8 6 5 4 6 7 6 Moving averages: 5 4 5 5 6 8 8 8 7 7 4 5 5 6 6 ```

Last edited on