managing memory in C, 2D float array - c

I'm very inexperienced with C and have to use it for a piece of coursework exploring heat transfer.
I'm getting all sorts of errors with the (0xC0000005) return code. I'm aware that this is an attempt to access memory out of bounds and that I'm probably failing to allocate memory properly somewhere but I haven't been able to figure it out.
Any idea what needs changing?
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main(){
/* constant alpha squared, the area of the domain */
const float alphSqrd = 0.01;
const float deltX, deltY = 0.0002;
const float timeStep = 0.0000009;
const int maxTimeSteps = 1000;
int h, i, j;
/* 2D Array of Temperature Values */
float T [500][500];
float dTdt;
/* initialise temperature values */
printf("Initialising 2D array...\n");
for (i=0; i<500; i++){
for (j=0; j<500; j++){
if (150<=i && i<350 && 150<=j && j<350){
T[i][j] = 50;
} else {
T[i][j] = 20;
}
}
}
printf("Updating values...\n");
for (h=0; h<maxTimeSteps; h++){
for (i=0; i<500; i++){
for (j=0; j<500; j++){
dTdt = alphSqrd*(((T[i+1][j]-2*T[i][j]+T[i-1][j])/(deltX*deltX))+((T[i][j+1]-2*T[i][j]+T[i][j-1])/(deltY*deltY)));
T[i][j] = T[i][j] + dTdt * timeStep;
printf("%f ",T[i][j]);
}
printf("\n");
}
}
return 0;
}

Although that's not the problem you're describing, one issue is that you're not initializing deltX. Instead of this
const float deltX, deltY = 0.0002;
you want
const float deltX = 0.0002 , deltY = 0.0002;
Aside from that, you have an out of range issue. If you're going to access index i - 1 and i + 1, you can't loop from 0 to 499 on an array of 500 elements.
It works for me if I adjust the loops like this:
for (i = 1; i < 499; i++) {
for (j = 1; j < 499; j++) {

During your dTdt calculation you are using T[i-1][j] and T[i-1][j]. If i is maximal (0 or 500) this exceeds the array limits. The same holds for j.
Thus, you use uninitialised memory. You need to loop form 1 to 499 and treat the boundary differently depending on the problem you are trying to solve.

Firstly adjust the loop min and max values;
for (i=1; i<499; i++){
for (j=1; j<499; j++){
And you should make comment line to printf(). stdout takes much time in the loop.
for (h=0; h<maxTimeSteps; h++){
for (i=1; i<499; i++){
for (j=1; j<499; j++){
dTdt = alphSqrd*(((T[i+1][j]-2*T[i][j]+T[i-1][j])/(deltX*deltX))+((T[i][j+1]-2*T[i][j]+T[i][j-1])/(deltY*deltY)));
T[i][j] = T[i][j] + dTdt * timeStep;
//printf("%f ",T[i][j]);
}
//printf("\n");
}
}
I compiled this code on my virtual Linux and it took nearly 3 seconds.
enter image description here

Related

Imprecise average at run time

I am trying to solve a problem and I've run into a bit of an issue.
I have to find the running average of a series of numbers.
Example:
input 4 2 7
output 4 3 4.3333
Now here's the problem although I get the answer, it is not the precise answer.
Accepted Output: accuracy difference shown in the image
290.6666666667
385.4000000000
487.8333333333
477.4285714286
496.4444444444
...
523.8571166992
506.0454406738
495.3043518066
I can't find whats wrong. Some help would be highly appreciated.
#include<stdio.h>
main(){
int n;
printf("set:");
scanf("%d",&n);
float arr[n+1],resarr[n+1];
float sum=0;
for(int i=1; i<=n; i++){
scanf("%f",&arr[i]);
sum=arr[i]+sum;
float res= sum/(float)i;
resarr[i]=res;
}
int i=1;
while(i<=n) {
printf("%0.10f\n",resarr[i]);
i++;
}
return 0;
}
Here
for(int i=1; i<=n; i++){ }
you are trying to access out of bound array elements, this certainly causes undefined behavior as let's assume if n is 5 then you are accessing arr[5] also which doesn't exist.
C doesn't perform array boundary condition check, its programmer responsibility to not to access out of bound elements else it causes UB.
In C array index starts from 0 not from 1. So better start rotating loop from 0 to n. For e.g
for(int i=0; i<n; i++) {
scanf("%f",&arr[i]);
/* some code */
}
Code fails to achieve the desired accuracy as it is using float rather than double. #Some programmer dude
Typical float is precise to 1 part in 223. For printing to 0.0000000001, better to use double which is typically precise to 1 part in 253.
#include<stdio.h>
int main(void) {
//float arr[n + 1], resarr[n + 1];
//float sum = 0;
double arr[n + 1], resarr[n + 1];
double sum = 0;
...
// scanf("%f", &arr[i]);
scanf("%lf", &arr[i]);
...
// float res = sum / (float) i;
double res = sum / i; // cast not needed as `sum` is `double`
...
}
Iterating from 1 is not idiomatic in C. More common to iterate starting at 0.
size_t is best for array sizing and indexing. int may be too narrow. Of course with small arrays, it makes scant difference.
#include<stdio.h>
int main(void) {
printf("set:");
size_t n;
scanf("%zu", &n);
double arr[n], resarr[n];
double sum = 0;
for (size_t i = 0; i < n; i++) {
scanf("%lf", &arr[i]);
sum = arr[i] + sum;
double res = sum / (i+1);
resarr[i] = res;
}
for (size_t i = 0; i < n; i++) {
printf("%0.10f\n", resarr[i]);
}
return 0;
}
More robust code would check the input from the user it insure it is valid, allocate rather than use a VLA if n is allowed to be large, flush output before reading, etc.
Note that array arr[] is not needed, just a single double for the input and sum.

C : Runtime Error Verdict (Help debugging)

I am working on a problem on an Online Judge ( link here ), and I keep getting a runtime error verdict each time I submit my solution. I have been looking at it for a while now, I still can't figure out what's wrong with it. I have also looked up the typical reasons behind a runtime error (arrays out of bonds, memory limit exceeded, logical errors ... ect). I can't find any of them in the code posted below :
#include<stdio.h>
int Hall[110][110];
int isValid(int x, int y){
int i,j,cnt=0;
for(i=x-1; i<=x+1; i++)
for(j=y-1; j<=y+1; j++)
if(Hall[x][y]==Hall[i][j])
cnt++;
if(cnt>1)
return 0;
else
return 1;
}
int notChecked(int N,int* checked,int size){
int i;
for(i=0; i<size; i++)
if(checked[i] == N)
return 0;
return 1;
}
int main (void){
int T,R,C,checked[110],i,j,k,size;
scanf("%d",&T);
for(i=0; i<T; i++){
scanf("%d %d",&R,&C);
//initialize
size=0;
for(j=0; j<R*C; j++) checked[j] = -1;
for(j=0; j<=C+1; j++){
Hall[0][j] = -1;
Hall[R+1][j] = -1;
}
for(j=0; j<=R+1; j++){
Hall[j][0] = -1;
Hall[j][C+1] = -1;
}
//read input
for(j=1; j<=R; j++)
for(k=1; k<=C; k++)
scanf("%d",&Hall[j][k]);
//algo:
for(j=1; j<=R; j++)
for(k=1; k<=C; k++)
if(Hall[j][k]>=0){
if(notChecked(Hall[j][k],checked,size)){
if(isValid(j,k)==0){
checked[size] = Hall[j][k];
size++;
}
}
}
printf("%d\n",size);
}
return 0;}
Any help is much appreciated !
You need to do bounds checking on your input.
According to the site T needs to be between 1 and 100 inclusive, so you need to check for that. Then for each R and C (N and M on the site), they also need to be between 1 and 100 inclusive.
For each set of R by C entries, you need to make sure you actually read in R rows and C columns. You also need to ensure that you read in T total sets of entries.
Problem Solved !
The code contained an array out of bonds problem, in the following line:
for(j=0; j<R*C; j++) checked[j] = -1;
Knowing that checked had a size of 110 and R and C a max size of 100 each. I changed the size of checked to 10100 and it worked.
Thanks #chux for the help !

Transfering a 2-dimensional array into a single dimension array

I'm trying to make a program in C that transfers a 2-dimensions-array(a matrix to be particular) into a single-dimension-array. For example, if we have a matrix with L lines and C columns, it should turn into a a single line newL=L*C. Therefore, if the matrix has 3 lines and 4 columns, the new array will have 3*4=12 as its size.
The matrix should turn to this:
1 2
--> 1 2 3 4
3 4
The problem I'm facing right now, is how to assign the matrix to the array without having random values or repeated values.
The piece of code I'm concerned with, goes like this:
for(k=1;k<=t;k++)
{
for(i=1;i<=l;i++)
{
for(j=1;j<=c;j++)
{
v[k]=m[i][j];
}
}
}
k,i and j are counters of the matrix(2-dimensions-array) and the the array. two of which; i and j, are counters for the matrix and k is the array's counter. Notice that each one of them starts from 1 and goes to its size and in this size I will use 2 lines and 2 columns for the matrix therefore the array will have a size of 4(2*2).
l is the number of lines in the array.
c is the number of colunms in the array.
t is the size of the array. t=l*c
Executing the code gives me this as a return:
1 2
--> 4 4 4 4
3 4
Simply said, the piece of code will ALWAYS give the last value of the matrix to the array. So if I replace 4 with 5 in the matrix, the array will have 5 5 5 5.
EDIT:
Here is the full code to understand what I'm trying to do:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int c,i,j,l,k,t;
printf("Donner le nombres des lignes: ");
scanf("%d",&l);
printf("Donner le nombres des colonnes: ");
scanf("%d",&c);
int m[l][c];
t=l*c;
int v[t];
for(i=0;i<l;i++)
{
for(j=0;j<c;j++)
{
printf("Donner m[%d][%d]: ",i+1,j+1);
scanf("%d",&m[i][j]);
}
}
for(i=0;i<l;i++)
{
for(j=0;j<c;j++)
{
printf("%d\t",m[i][j]);
}
printf("\n");
}
printf("\n\n\n\n");
for(k=1;k<=t;k++)
{
for(i=1;i<=l;i++)
{
for(j=1;j<=c;j++)
{
v[k]=m[i][j];
}
}
}
for(k=0;k<t;k++)
{
printf("%d\t",v[k]);
}
system("pause");
}
Thank you guys for the help, I found the correct way to do it.
You need not the outer loop
Array indices are zero-based in C
Thus, we have:
for(k = 0, i = 0; i < o; i++)
{
for(j = 0; j < p; j++)
{
v[k++] = m[i][j];
}
}
where o and p - dimensions of the matrix m
If we have a multidimensional array like this:
int nums[3][3];
And we have:
int all[9];
And we've got:
int a, b;
We'll reference each of the nums like this:
nums[a][b];
Now think of what the values of a and b will actually be:
for (a = 0; a < 3; a++) {
for (b = 0; b < 3; b++)
all[((a * 3) + b)] = nums[a][b];
}
This will work so long as you multiply a with the number of elements it will iterate:
int nums[5][5];
int all[25];
int a, b;
for (a = 0; a < 5; a++) {
for (b = 0; b < 5; b++)
all[((a * 5) + b)] = nums[a][b];
}
You mention your question is "how to I fix the code?" I think plenty of people have given you the correct answer. This is your code along with the corrected code.
#include <stdio.h>
#include <stdlib.h>
int main()
{
int c,i,j,l,k,t;
printf("Donner le nombres des lignes: ");
scanf("%d",&l);
printf("Donner le nombres des colonnes: ");
scanf("%d",&c);
int m[l][c];
t=l*c;
int v[t];
for(i=0;i<l;i++)
{
for(j=0;j<c;j++)
{
printf("Donner m[%d][%d]: ",i+1,j+1);
scanf("%d",&m[i][j]);
}
}
for(i=0;i<l;i++)
{
for(j=0;j<c;j++)
{
printf("%d\t",m[i][j]);
}
printf("\n");
}
printf("\n\n\n\n");
/* corrected code below */
k = 0;
for(i=0;i<l;i++)
{
for(j=0;j<c;j++)
{
v[k]=m[i][j];
k++;
}
}
/* corrected code above */
for(k=0;k<t;k++)
{
printf("%d\t",v[k]);
}
system("pause");
}
As long as the new array is the correct size, something like the following should work:
k=0;
for(i=0;i<l;i++){
for(j=0;j<c;j++){
v[k]=m[i][j];
k++;
}
}
Essentially, you are traversing over the matrix (your lines and columns--as you put it) and at the same time increasing the position (k) in the new array where you want that value to be put.
This:
for(k=1;k<=t;k++)
for(i=1;i<=l;i++)
for(j=1;j<=c;j++)
v[k]=m[i][j];
does not do what you think. Think about when you first loop through the j part, you will be setting all the 0th element of v the entire time, finally the last value you set will stick (ie, the one in position 1, 1 which happens to be 4). Then you will increment k to 1 and repeat it again, resulting in all 4's. You want this:
for(i = 0; i < l; i++)
for(j = 0; j < c; j++)
v[i*l+j] = m[i][j]; // i*l + j gives you the equivelent position in a 1D vector.
Make sure your v vector is the right size ie. int v[l*c];. Also remember that in c zero indexing is used.If you really do need 1 based indexing (which you dont ...) then do this:
int k = 1;
for(i = 1; i <= l; i++)
for(j = 1; j <= c; j++)
v[k++]=m[i][j];
But remember that this will make any further operations on this vector Gross. So dont do this ....
If you just want to access the matrix elements as a single dimension array, you could declare an int pointer v:
int m[3][4];
int *v = (int*)m;
// then access for example m[1][1] as v[5]
Or, to actually copy the array, use a double for (as in the other answers), a single for like below
int vv[12];
for(i = 0; i < 12; i++)
vv[i] = m[i/4][i%4];
or just use memcpy:
memcpy(vv, m, 12*sizeof(int));

C Array of Counters

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main()
{
int i; //Counting Variable for loop
int sales[30]; //Array for sales people
//Creates Gross for 30 Indiviudals
for (i = 0; i < 30; i++)
{
sales[i] = ( rand() % 15000) + 1;
}
printf("Original List\n");
for (i = 0; i < 30; i++)
{
printf("%d\n", sales[i]);
}
return 0;
}
Im tying to make a program that takes 30 random numbers between 0 and 15000, then applies the equation (100+a*0.09), then sorts the answers from highest to lowest. Im getting stuck on how to apply the equation to the values founded in the array since they are program generated.
you can just loop through the array again and assign them again just like you looped through the two previous times
for (i=0; i < 30; i++){
sales[i] = 100 + sales[i] * .09;
}
You mean:
for (i = 0; i < 30; i++)
{
printf("%f\n", 100 + (float)sales[i] * 0.09);
}
?
You can also store it in an other array (of float/double). Not sure to understand correctly as it seems very simple!
Edit: if you need to sort it you would have to store it in an array (float vals[30]; let say) and then have a look to 'qsort(…)' function in order to order the values.

Why do I get same addresses in 2D array?

Recently I came to this problem, when assigning values to 2D array. To represent my problem I created small C code. I am using QT creator (community) 3.3.0 and minGW 4.9.1 32bit.
#include <stdio.h>
int main(void){
double m[2][2];
for (int i = 0 ; i<3; i++){
for(int j=0; j<3; j++)
printf("%p[%d][%d] ", (void*)&m[i][j],i,j);
printf("\n");
}
return 0;
}
As output I get memory addresses
0028FE98[0][0] 0028FEA0[0][1] 0028FEA8[0][2]
0028FEA8[1][0] 0028FEB0[1][1] 0028FEB8[1][2]
0028FEB8[2][0] 0028FEC0[2][1] 0028FEC8[2][2]
You can see that there are same addresses for non-equal array items. So when I assign value to for example [1][2], value in [2][0] changes too.
Please give me any advice on how to solve this. Thank you very much.
Your array should either be 3x3:
double m[3][3];
Or your loop should only iterate twice, not three times.
The code you have now accesses some out of bounds memory, causing undefined behaviour.
Your array size is too small.
#include <stdio.h>
int main(void){
double m[3][3]; // <------ Change size to 3,3
for (int i = 0 ; i<3; i++){
for(int j=0; j<3; j++)
printf("%p[%d][%d] ", (void*)&m[i][j],i,j);
printf("\n");
}
return 0;
}
you declare m[2][2] to have 2 rows of 2, valid indexes are 0 and 1
if you want a 3x3 array you have to declare
double m[3][3];
You have wrong for conditions. Should be i<2 and j<2. Try and you'll se that all will be ok.
And why there're same pointers? Because table(1d, 2d, 3d don't matter) in memory is one memory block. All dimensions are "in one line" and when you write something like this
double tab[2][2];
tab[0][1] = 1; // that's the same as *(tab + 0*sizeof(double)*2 + 1*sizeof(double)) = 1;
tab[1][1] = 2; // that's the same as *(tab + 1*sizeof(double)*2 + 1*sizeof(double)) = 2;
So you can see that you have this
double m[2][2];
m[1][2] // that's the same as (m + 1*sizeof(double)*2 + 2*sizeof(double)) => (m + 4*sizeof(double)) ;
m[2][0] // that's the same as (m + 2*sizeof(double)*2 + 0*sizeof(double)) => (m + 4*sizeof(double));
That's why that was the same pointers

Resources