Thread: Copying row array help!!

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    1

    Copying row array help!!

    Hi everyone!

    I have a c program that will create a 2xm array, and my goal is to have the second row increment from zero to m and then have the first row copy the second row (i could increment the first row too, but for practice, i want to see if i can copy the data in the second row to the first row.

    here is my code:

    Code:
    #include <stdlib.h>
    #include <stdio.h>
    
       int main(){
       
          int s2 = 5;
          int array[2][s2];
          int j;
          int i;
       
       
       //initialize the first row to zeros
          for(j = 0; j < s2 + 1; j++){
             array[0][j] = 0;
          }
       
    	//make the second row an increasing increment
          for(j = 0; j < s2 + 1; j++){
             array[1][j] = j;
          }
       	
    	//print out the 2m array
          for(i = 0; i < 2; i++){
             for(j = 0; j < s2 + 1; j++){
                printf("%d\t", array[i][j]);
             }
             printf("\n");
          }		
       	
          printf("\n\n");
       
    	//copy the second row to the first row
          for(j = 0; j < s2+1; j++){
             array[0][j] = array[1][j];
          }	
       
    	//print out the 2m array	   	
          for(i = 0; i < 2; i++){
             for(j = 0; j < s2 + 1; j++){
                printf("%d\t", array[i][j]);
             }
             printf("\n");
          }		
       	
       	
          return 0;
       }
    the data that i get is:
    0 1 2 3 4 5
    5 1 2 3 4 5

    and i don't get why the first columns aren't the same..

    i hard coded the m in 2xm for better reading..
    any help is appreciated!!

  2. #2
    Registered User
    Join Date
    Jun 2010
    Location
    In a house
    Posts
    15
    You are doing it wrong with the s2+1, size is 5 so it should be s2.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Copying an array into another
    By Alienchicken in forum C Programming
    Replies: 0
    Last Post: 04-13-2011, 09:55 PM
  2. Array Copying
    By Prodiga1 in forum C Programming
    Replies: 2
    Last Post: 03-01-2011, 04:18 PM
  3. copying to an array
    By tedderry in forum C Programming
    Replies: 4
    Last Post: 01-19-2011, 06:06 PM
  4. need help in copying unsigned char array to bit array
    By lovesunset21 in forum C Programming
    Replies: 8
    Last Post: 10-29-2010, 07:23 AM
  5. Copying array's
    By big146 in forum C++ Programming
    Replies: 4
    Last Post: 06-15-2004, 01:25 PM

Tags for this Thread