How to copy the content of two arrays in third?

This is a discussion on How to copy the content of two arrays in third? within the C Programming forums, part of the General Programming Boards category; The goal of the exercise is: Two files FA and FB whose names are entered on the keyboard contain integers ...

  1. #1
    Registered User
    Join Date
    Dec 2010
    Posts
    1

    How to copy the content of two arrays in third?

    The goal of the exercise is:

    Two files FA and FB whose names are entered on the keyboard contain integers sorted in ascending order. Write a program that copies the contents of FA and FB, respectively, in the dynamic tables TABA and TABB in main memory. Tables TABA and TABB are merged into a third sorted array in ascending TABC. After the merger, the TABC table is saved in a file whose name is FC to enter the keyboard. Memory for TABA, TABB TABC and whose numbers of elements are unknown, is reserved dynamically.

    How to copy the content of TABA and TABB to a new array TABC?

    Code:
    #include<stdio.h>
    #include <stdlib.h>
    
    int *lire(char *nom_fichier,int *c);
    void print(int *a,int n);
    
    
    void main(void)
    {    int n, i, *TABA, *TABB, *TABC;
         char nom_fichier[10], nouveau_fichier[10];
    
         printf("Entrer le nom du premier fichier: ") ;
         scanf("%s", &nom_fichier);
         TABA=lire(nom_fichier,&n);
    
         printf("\nLe contenu du premier fichier:\n\n");
         print(TABA,n);
    
         printf("\nEntrer le nom du deuxiem fichier: ") ;
         scanf("%s", &nom_fichier);
         TABB=lire(nom_fichier,&n);
    
         printf("\nLe contenu du deuxiem fichier:\n\n");
         print(TABB,n);
    
    
    }
    
    int* lire(char *nom_fichier,int *c)
    {    FILE *p;
         int x, i,*tab, *t;
         *c=0;
         p=fopen(nom_fichier,"r");
         if(p==NULL)
    	{ printf("Le fichier %s n'est pas ouvert!\n",nom_fichier); exit(1); }
         fscanf(p,"%d",&x);
         while(!feof(p))
    	{     (*c)++;
    	      fscanf(p,"%d",&x);
    	}
         tab=(int *)malloc(*c*sizeof(int));
         if(tab==NULL)
    	{ printf("Allocation impossible!\n"); exit(1); }
         t=tab;
         rewind(p);
         *c=0;
         fscanf(p,"%d",tab);
         while(!feof(p))
    	{    (*c)++;
    	     fscanf(p,"%d",++tab);
    	}
         fclose (p);
         return t;
    }
    
    
    void print(int *a,int n)
    	{  int i;
    	   for(i=0;i<n;i++,a++)
    		printf("%3d",*a);
    	   printf("\n");
    	}

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    You were given a big hint in the directions: "merged". You should see where that gets you.

  3. #3
    Registered User
    Join Date
    Aug 2010
    Posts
    231
    Code:
    char nom_fichier[10], nouveau_fichier[10], copyresult[20];
    ...
    /* for strings you can take: */
    strcpy(copyresult,nom_fichier);
    strcat(copyresult,nouveau_fichier);
    /* for real arrays you must take: */
    memcpy(copyresult,nom_fichier,sizeof nom_fichier);
    memcpy(copyresult+sizeof nom_fichier,nouveau_fichier,sizeof nouveau_fichier);

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by BillyTKid View Post
    /* for real arrays you must take: */
    That's funny, I could have sworn I was allowed to access arrays one element at a time. Also, the pedant in me takes exception to your use of the term "real arrays".


    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Copying constant amount of data
    By TriKri in forum C++ Programming
    Replies: 16
    Last Post: 07-12-2008, 06:32 AM
  2. Dynamic (Numeric) Arrays
    By DavidB in forum C++ Programming
    Replies: 5
    Last Post: 05-03-2006, 07:34 PM
  3. Need Help With 3 Parallel Arrays Selction Sort
    By slickwilly440 in forum C++ Programming
    Replies: 4
    Last Post: 11-19-2005, 09:47 PM
  4. Building B-Tree from Arrays
    By 0rion in forum C Programming
    Replies: 1
    Last Post: 04-09-2005, 02:34 AM
  5. Help with arrays and pointers please...
    By crazyeyesz28 in forum C++ Programming
    Replies: 8
    Last Post: 03-17-2005, 12:48 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21