hi i have created a code to merge two char arrays for assignment plz can u guide me is it ok.how much marks can i get 4 it out of 20.

#include <iostream>
using namespace std;

//declaring the functions 

void merge(char[],int, char[], int,char[],int);
void sort(char[] ,int);
void display(char[],int);

main()
{
      //declaring the arrays
      char a[10];
      char b[10];
      char result[20];
      int size;
      cout <<"please enter the ten alphabets ";
      for (int i=0;i<=9;i++)
      {
          cin >>a[i];
          }
          cout <<"please enter the elements of second array:";
          for (int j=0;j<=9;j++)
      {
          cin >>b[j];
          }
          
          merge(a,10,b,10,result,size);
          sort (result,size);
          display (result, size);
}
//----------merging-----------
void merge (char array1[],int size1, char array2[],int size2, char c[],int *size )
{
     for (int k=0;k< size1;k++)
     {
         c [k] =array1[k];
         }
         for (int k=0;k<size2;k++)
         {
             c [size1+k] = array2[k];
             
             *size=size1+size2;
             }
             
}  
//--------------------sorting----------------
void sort(char c[],int size)
{
     int end=size-1;
     for (int i=0;i<=size;i++)
     {
         for (int j=0;j<=end;j++)
         {
             if (c[j]>c[j+1])
             {
                             char temp=c[j];
                             c[j]=c[j+1];
                             c[j+1]=temp;
                             }
                             }
                             }
                             }
     
     
     
//---------------display output---------------
void display (char c[],int num)
{
     for (int l=0;l<num;l++)
     cout <<c[l];
     }

Dani AI

Generated

A few practical notes that build on 's pointer/reference suggestion and clean up some bugs in the posted code.

The big problem that causes undefined behavior is a prototype/definition mismatch for merge and the uninitialized size passed from main. Also the sorting loops as written can index past the end (accessing c[j+1] when j reaches the last valid index). Other small issues: main should be declared int main(), prefer size_t for sizes, avoid magic constants (use array sizes or container sizes), and remember to initialize or null-terminate C-style strings if you treat them that way.

Concrete, low-effort improvements:

  • Make the merge prototype match its definition. Return the merged length by int& or int*, or simply return a container (recommended).
  • Set the merged size once after copying both arrays, not inside the copy loop.
  • Fix the bubble-sort bounds: do for (i = 0; i + 1 < n; ++i) and for (j = 0; j + 1 < n - i; ++j) to avoid j+1 running past n-1.
  • Prefer standard containers and algorithms: std::string or std::vector<char> plus std::sort or std::merge will be clearer and less error-prone (see std::sort and std::merge).

Minimal modern approach (different from the posted code) that avoids most pointer/loop bugs:

#include <iostream>
#include <string>
#include <algorithm>

int main() {
    std::string a, b;
    std::cin >> a >> b;           // read two tokens
    std::string merged = a + b;
    std::sort(merged.begin(), merged.end());
    std::cout << merged << '\n';
    return 0;
}

For grading: correctness, robustness (input validation and bounds safety), clear use of language features, and readable style matter more than clever but fragile hand-rolled loops. Fixing the prototype/UB and the sort bounds will move this from "buggy" to "acceptable"; switching to std::string/std::sort makes it clean and likely scores higher.

Recommended Answers

All 2 Replies

line 6: should be:

void merge(char[],int, char[], int,char[],int*);

better:

void merge(char[],int, char[], int,char[],int&);
//----------merging-----------
void merge (char array1[],int size1, char array2[],int size2, char c[],int &size )
{
     for (int k=0;k< size1;k++)
     {
         c [k] =array1[k];
         }
         for (int k=0;k<size2;k++)
         {
             c [size1+k] = array2[k];
             
             size=size1+size2;
             }
             
}

Did you look at c++ algorithm like merge?

ok thanx and is the rest fine? n how much marks can i get 4 this program out of 20marks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.