Thread: 1d array into 2d array

  1. #16
    Registered User
    Join Date
    Feb 2013
    Posts
    41
    One last question and again thanks everyone for your help I'm just impatient and instead of waiting till Monday to hear back from my teacher I was hoping someone here might know. Is there a limitation of the number of integers C can display on one line? It seems that even though I want 4 rows it put a 2 more rows because it is not able to list 25 integers on one line. Or is this a limitation of visual studio? Thanks again. I put the full code in case anyone wants to see what it prints out.
    Code:
    void main()
    {
     int change;
     int i = 0;
     int j = 0;
     int array [100] = {0};
      for(i = 0; i<100; i++)
      {
     
       array[i] = rand();
     
      } 
     
     
     change = changeA(array);
     
    }
    int changeA(int array[100])
    {
     int i = 0;
     int j = 0;
     int array2[4][25] = {0};
      for(i = 0; i < 4; i++)
      {
      
       for(j = 0; j<24; j++)
        {
         array2[i][j] = array[i*25+j];
         printf("%2d %2d", i,j);
        
        }
      }
     printf("\n\n");
     
     
     system("pause");
    }
    


  2. #17
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    You might want to put that system pause in your main (or replace it with getchar())

    Change your "void main" to "int main" and add a "return 0;" at the end of your main
    Fact - Beethoven wrote his first symphony in C

  3. #18
    Registered User
    Join Date
    Feb 2013
    Posts
    41
    Thanks Click_here changed that but I'm stuck lol. I'll wait to see when the Prof. gets back to me I'm just burnt out been messing with this since 3:00pm central time lol. Thanks again for everyone help def got me moving along I'll keep you guys updated
    Code:
    int changeA(int array[]);
    
    int main()
    {
     int change;
     int i = 0;
     int j = 0;
     int array [100] = {0};
      for(i = 0; i<100; i++)
      {
     
       array[i] = rand();
     
      } 
     
     
     change = changeA(array);
     system("pause");
     return 0;
    }
    int changeA(int array[100])
    {
     int i = 0;
     int j = 0;
     int array2[4][25] = {{0}};
      for(i = 0; i < 4; i++)
      {
     
       for(j = 0; j<24; j++)
        {
         array2[i][j] = array[i*25+j];
         printf("%d %d", array2[i][j]);
        
        }
      }
     printf("\n\n");
     
     
     
    }

  4. #19
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Quote Originally Posted by phillyflyers View Post
    Is there a limitation of the number of integers C can display on one line? It seems that even though I want 4 rows it put a 2 more rows because it is not able to list 25 integers on one line.
    How many numbers there are on one line is dependent on the number of digits and the horizontal size of your terminal. It has nothing to do with C.

    Code:
    for(j = 0; j<24; j++)
    The highest value of "j" will be 23 thus you aren't assigning a value to the 25th element (index 24) in each row.

    Code:
    printf("%d %d", array2[i][j]);
    You should get a warning here (do you know how to turn on warnings for your compiler?). You have two format specifiers but only one value to print.

    Bye, Andreas

  5. #20
    Registered User
    Join Date
    Feb 2013
    Posts
    41
    I have visual studio 2012 and I am not get any warnings it just prints out nonsense. I tried doing what my Prof. replied with I'm not sure if the program is correct, because it still is printing on more than 4 rows. Is this just cause its the way I have it programmed wrong or just how visual studio 2012 shows it? Thanks.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
     
    int changeA(int array[]);
    
    int main()
    {
     int change;
     int i = 0;
     int j = 0;
     int array [100] = {0};
      for(i = 0; i<100; i++)
      {
     
       array[i] = rand();
     
      } 
     
     
     change = changeA(array);
     system("pause");
     return 0;
    }
    int changeA(int array[100])
    {
     int i = 0;
     int j = 0;
     int array2[4][25] = {{0}};
      for(i = 0; i < 4; i++)
      {
     
       for(j = 0; j<24; j++)
        {
         array2[i][j] = array[i*25+j];
         printf("%6d", array2[i][j]);
        
        }
      }
     printf("\n\n");
     return array2[i][j]

  6. #21
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Quote Originally Posted by phillyflyers View Post
    I have visual studio 2012 and I am not get any warnings it just prints out nonsense.
    How to enable warnings in visual studio 2012

    You have to be more specific with your questions. What's "nonsense" and what do you expect as the correct output?

    Your program is printing more than 4 rows because 25 numbers probably don't fit into one line of your terminal and you never print a newline between the numbers.

    Bye, Andreas
    Last edited by AndiPersti; 04-13-2013 at 09:56 AM.

  7. #22
    Registered User
    Join Date
    Feb 2013
    Posts
    41
    Hi Andi, thank you i was missing this one line it now prints it right. It is the terminal that makes it look like it isnt doing the numbers on 4 rows.
    Code:
    for(i = 0; i < 4; i++)
      {
       printf("\n");
       for(j = 0; j<24; j++)
        {
         array2[i][j] = array[i*25+j];
         printf("%6d", array2[i][j]);
         
        
        }

  8. #23
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    You still do not print the last element of each row.

    Bye, Andreas
    Last edited by AndiPersti; 04-13-2013 at 02:13 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 05-09-2012, 06:41 AM
  2. Replies: 2
    Last Post: 03-20-2012, 08:41 AM
  3. Replies: 9
    Last Post: 08-23-2010, 02:31 PM
  4. Replies: 6
    Last Post: 11-09-2006, 03:28 AM
  5. Replies: 1
    Last Post: 04-25-2006, 12:14 AM