The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> C Programming
|
2D array filling
Discuss 2D array filling in the C Programming forum on Dev Shed. 2D array filling C programming forum discussing all C derivatives, including C#, C++, Object-C, and even plain old vanilla C. These languages are low level languages, and used on projects such as device drivers, compilers, and even whole computer operating systems.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

November 16th, 2012, 11:49 AM
|
|
Registered User
|
|
Join Date: Nov 2012
Posts: 4
Time spent in forums: 1 h 43 m 46 sec
Reputation Power: 0
|
|
|
2D array filling
Hi, I need to fill 2D array with random numbers
Code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main()
{ int i, j, n, m, *rod;
srand(time(NULL));
n=rand()%10+1;
m=rand()%10+1;
printf("size %d x %d\n",n,m);
rod=(int*)malloc(n*m*sizeof(int));
for(i=0;i<=n;i++)
for(j=0;j<=m;j++)
*(rod+j)=rand()%10+1;
for(i=0;i<=n;i++)
for(j=0;j<=m;j++)
printf("%d\n",*(rod+j));
system("pause");
return 0; }
I think my code doesn't work properly, any ideas? 
|

November 16th, 2012, 12:27 PM
|
 |
Java Junkie
|
|
Join Date: Jan 2004
Location: Mobile, Alabama
|
|
One thing I notice here is that when you place an element in the array, you don't use the value of i.
Code:
for(i=0;i<=n;i++)
for(j=0;j<=m;j++)
*(rod+j)=rand()%10+1;
|

November 16th, 2012, 12:32 PM
|
 |
Contributing User
|
|
|
|
Observe how I used k. Most programmers, I expect, would prefer to increment k as a separate statement.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main() {
int k;
int i, j, n, m, *rod;
srand(time(NULL));
n=rand()%10+1;
m=rand()%10+1;
printf("size %d x %d\n",n,m);
rod=(int*)malloc(n*m*sizeof(int));
k = 0;
for(i=0;i<n;i++)
for(j=0;j<m;j++)
*(rod+k++)=rand()%10+1;
puts("in-order access");
k = 0;
for(i=0;i<n;i++) {
for(j=0;j<m;j++)
printf(" %2d",*(rod+k++));
putchar('\n');
}
puts("random access");
for(i=0;i<n;i++) { /* i <= n was incorrect */
for(j=0;j<m;j++)
printf(" %2d",*(rod+j+i*m));
putchar('\n');
}
# ifndef __unix__
system("pause");
# endif
return 0;
}
__________________
[code] Code tags[/code] are essential for python code!
|

November 16th, 2012, 12:33 PM
|
|
|
|
In addition to what bullet said, I add
+ work on your indentation
+ the cast to the return value of malloc() is, at best, redundant; and it may hide an error (not in your code as presented) the compiler would catch in the absence of the cast
+ your loops are going once more than they should. The idiomatic way to loop is for (k = 0; k < 10; k++). Your condition inside the loop has a less than or equal operator.
+ you didn't free the allocated memory therby causing a memory leak.
|

November 16th, 2012, 12:36 PM
|
 |
Contributing User
|
|
Join Date: Jan 2003
Location: USA
|
|
For one thing, even though you used code tags in your very first post here (extremely laudable, that!), you completely and utterly failed to format your code. The purpose of code tags is to preserve your code's formatting, so obviously you should have formatted it. All you accomplished was to completely defeat the purpose of using code tags, so why even bother?
Code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main()
{
int i, j, n, m, *rod;
srand(time(NULL));
n=rand()%10+1;
m=rand()%10+1;
printf("size %d x %d\n",n,m);
rod=(int*)malloc(n*m*sizeof(int));
for(i=0;i<=n;i++)
for(j=0;j<=m;j++)
*(rod+j)=rand()%10+1;
for(i=0;i<=n;i++)
for(j=0;j<=m;j++)
printf("%d\n",*(rod+j));
system("pause");
return 0;
}
See what a difference that makes?
Second, you're not using a 2-D array, but rather trying to emulate one with a simple array. I guess that could work, but if you want to use a 2-D array, then why not simply use one?
Third, your for-statement test expressions are wrong, causing you to insert an extra loop. To range over n values, you index with 0 through n-1. If instead you do as you have done, you're indexing with 0 through n, which means that you'll loop n+1times. That does not work.
Fourth (though it should have been first or second), what do you "my code doesn't work properly"? Since we cannot read your mind, we have no idea what you are talking about. You see, you need to tell us these things!
If it doesn't compile or gives you warnings, then tell us what those messages are (copy-and-paste is a good idea). Also telling us what compiler you're using can be very helpful, as would the operating system.
If it compiles but crashes when you try to run it, then tell us that. Also, if it seems to crash in response to certain inputs, then provide us with those inputs.
If it runs and doesn't crash but does not give you the output you expect, then describe to us:
a. what that wrong output is,
b. what output you were expecting, and
c. what inputs you gave it that produced that output.
For us to be able to help you, you need to help us.
|

November 16th, 2012, 01:09 PM
|
|
Registered User
|
|
Join Date: Nov 2012
Posts: 4
Time spent in forums: 1 h 43 m 46 sec
Reputation Power: 0
|
|
Ok, sorry guys for the mess (and for bad grammar  ), next time i will do better. And thanks to
b49P23TIvg it helped me 
|

November 16th, 2012, 02:24 PM
|
|
Registered User
|
|
Join Date: Nov 2012
Posts: 4
Time spent in forums: 1 h 43 m 46 sec
Reputation Power: 0
|
|
Now I want in each line find the smallest number, I tried with this code:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main()
{
int i, j, n, m, k, *min, *rod;
srand(time(NULL));
n=rand()%10+1;
m=rand()%10+1;
printf("lines %d columns %d\n",n,m);
rod=(int*)malloc(n*m*sizeof(int));
k = 0;
for(i=0;i<n;i++)
for(j=0;j<m;j++)
*(rod+k++)=rand()%10+1;
free(rod);
k = 0;
for(i=0;i<n;i++)
{
printf("\n");
for(j=0;j<m;j++)
printf(" %d",*(rod+k++));
printf("\n");
}
//------------------------------------------//
k=0;
min=rod;
*min=*rod;
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
if (*(rod+k++) < *min)
*min = *(rod+k++);
printf("smallest number in the line: %d\n", *min);
}
system("pause");
return 0;
}
but always i get zero's
http://img838.imageshack.us/img838/1096/llllv.jpg
what should I do?
Thanks for the help 
|

November 16th, 2012, 02:31 PM
|
|
|
|
You cannot (safely) use rod after you free() it.
Move the free() to the end of your program, after all uses of rod.
Other than that I haven't checked the workings of your program
|

November 16th, 2012, 02:39 PM
|
|
Registered User
|
|
Join Date: Nov 2012
Posts: 4
Time spent in forums: 1 h 43 m 46 sec
Reputation Power: 0
|
|
|
Now when I moved free() I get nonses like this:
goo.gl/cxnIf
|

November 16th, 2012, 02:45 PM
|
 |
Contributing User
|
|
|
|
You do not understand the rod+k++ expression.
And, having freely copied it you've run into big trouble.
Code:
if (*(rod+k++) < *min)
*min = *(rod+k++);
rod+k++ results in rod+k, and also incremented k. Oh!
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|