C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old November 16th, 2012, 11:49 AM
martynasj martynasj is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 4 martynasj User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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?

Reply With Quote
  #2  
Old November 16th, 2012, 12:27 PM
bullet's Avatar
bullet bullet is offline
Java Junkie
Dev Shed Expert (3500 - 3999 posts)
 
Join Date: Jan 2004
Location: Mobile, Alabama
Posts: 3,824 bullet User rank is General 4th Grade (Above 100000 Reputation Level)bullet User rank is General 4th Grade (Above 100000 Reputation Level)bullet User rank is General 4th Grade (Above 100000 Reputation Level)bullet User rank is General 4th Grade (Above 100000 Reputation Level)bullet User rank is General 4th Grade (Above 100000 Reputation Level)bullet User rank is General 4th Grade (Above 100000 Reputation Level)bullet User rank is General 4th Grade (Above 100000 Reputation Level)bullet User rank is General 4th Grade (Above 100000 Reputation Level)bullet User rank is General 4th Grade (Above 100000 Reputation Level)bullet User rank is General 4th Grade (Above 100000 Reputation Level)bullet User rank is General 4th Grade (Above 100000 Reputation Level)bullet User rank is General 4th Grade (Above 100000 Reputation Level)bullet User rank is General 4th Grade (Above 100000 Reputation Level)bullet User rank is General 4th Grade (Above 100000 Reputation Level)bullet User rank is General 4th Grade (Above 100000 Reputation Level)bullet User rank is General 4th Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 1 Week 6 Days 8 h 32 m 13 sec
Reputation Power: 1248
Send a message via ICQ to bullet Send a message via AIM to bullet Send a message via MSN to bullet
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;

Reply With Quote
  #3  
Old November 16th, 2012, 12:32 PM
b49P23TIvg's Avatar
b49P23TIvg b49P23TIvg is offline
Contributing User
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Aug 2011
Posts: 3,358 b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 3 Days 9 h 37 m 48 sec
Reputation Power: 383
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!

Reply With Quote
  #4  
Old November 16th, 2012, 12:33 PM
bdb bdb is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2012
Posts: 156 bdb User rank is Sergeant Major (2000 - 5000 Reputation Level)bdb User rank is Sergeant Major (2000 - 5000 Reputation Level)bdb User rank is Sergeant Major (2000 - 5000 Reputation Level)bdb User rank is Sergeant Major (2000 - 5000 Reputation Level)bdb User rank is Sergeant Major (2000 - 5000 Reputation Level)bdb User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 1 Week 15 h 48 m 11 sec
Reputation Power: 32
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.

Reply With Quote
  #5  
Old November 16th, 2012, 12:36 PM
dwise1_aol's Avatar
dwise1_aol dwise1_aol is online now
Contributing User
Dev Shed God 2nd Plane (6000 - 6499 posts)
 
Join Date: Jan 2003
Location: USA
Posts: 6,128 dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Months 2 Weeks 3 Days 18 h 42 m 39 sec
Reputation Power: 1949
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.

Reply With Quote
  #6  
Old November 16th, 2012, 01:09 PM
martynasj martynasj is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 4 martynasj User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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

Reply With Quote
  #7  
Old November 16th, 2012, 02:24 PM
martynasj martynasj is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 4 martynasj User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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

Reply With Quote
  #8  
Old November 16th, 2012, 02:31 PM
bdb bdb is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2012
Posts: 156 bdb User rank is Sergeant Major (2000 - 5000 Reputation Level)bdb User rank is Sergeant Major (2000 - 5000 Reputation Level)bdb User rank is Sergeant Major (2000 - 5000 Reputation Level)bdb User rank is Sergeant Major (2000 - 5000 Reputation Level)bdb User rank is Sergeant Major (2000 - 5000 Reputation Level)bdb User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 1 Week 15 h 48 m 11 sec
Reputation Power: 32
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

Reply With Quote
  #9  
Old November 16th, 2012, 02:39 PM
martynasj martynasj is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 4 martynasj User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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

Reply With Quote
  #10  
Old November 16th, 2012, 02:45 PM
b49P23TIvg's Avatar
b49P23TIvg b49P23TIvg is offline
Contributing User
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Aug 2011
Posts: 3,358 b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 3 Days 9 h 37 m 48 sec
Reputation Power: 383
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!

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > 2D array filling

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap