SlideShare a Scribd company logo
1 of 5
Download to read offline
// C Program to shuffle a given Aay
#include
#include
#include
// A utility function to print an Aay
void printArray (int A[], int n)
{
int i;
for (i = 0; i < n; i++)
printf("%d ", A[i]);
printf(" ");
}
int* shuffle(int A[], int B[])
{
int n1 = 5;
int n2 = 2;
int i = 0, j = 0, k = 0;
int *C;
C = (int*)malloc(7 * sizeof(int));
// Merging starts
while (i < n1 && j < n2)
{
if (k%2 == 0)
{
C[k] = A[i];
i++;
k++;
}
else
{
C[k] = B[j];
k++;
j++;
}
}
/* elements in array 'A' are still remaining
where as the array 'B' is exhausted */
while (i < n1)
{
C[k] = A[i];
i++;
k++;
}
/* elements in array 'B' are still remaining
where as the array 'A' is exhausted */
while (j < n2)
{
C[k] = B[j];
k++;
j++;
}
return C;
}
int main()
{
int A[] = {1, 2, 3, 4, 5};
int B[] = {10, 20};
int n1 = 5;
int n2 = 2;
int nC = 7;
int *C;
C = (int*)malloc(7 * sizeof(int));
printf("Array A: ");
printArray(A, n1);
printf("Array B: ");
printArray(B, n2);
C = shuffle (A, B);
printf(" Shuffled: ");
printArray(C,nC);
return 0;
}
/*
output:
Array A: 1 2 3 4 5
Array B: 10 20
Shuffled: 1 10 2 20 3 4 5
*/
Solution
// C Program to shuffle a given Aay
#include
#include
#include
// A utility function to print an Aay
void printArray (int A[], int n)
{
int i;
for (i = 0; i < n; i++)
printf("%d ", A[i]);
printf(" ");
}
int* shuffle(int A[], int B[])
{
int n1 = 5;
int n2 = 2;
int i = 0, j = 0, k = 0;
int *C;
C = (int*)malloc(7 * sizeof(int));
// Merging starts
while (i < n1 && j < n2)
{
if (k%2 == 0)
{
C[k] = A[i];
i++;
k++;
}
else
{
C[k] = B[j];
k++;
j++;
}
}
/* elements in array 'A' are still remaining
where as the array 'B' is exhausted */
while (i < n1)
{
C[k] = A[i];
i++;
k++;
}
/* elements in array 'B' are still remaining
where as the array 'A' is exhausted */
while (j < n2)
{
C[k] = B[j];
k++;
j++;
}
return C;
}
int main()
{
int A[] = {1, 2, 3, 4, 5};
int B[] = {10, 20};
int n1 = 5;
int n2 = 2;
int nC = 7;
int *C;
C = (int*)malloc(7 * sizeof(int));
printf("Array A: ");
printArray(A, n1);
printf("Array B: ");
printArray(B, n2);
C = shuffle (A, B);
printf(" Shuffled: ");
printArray(C,nC);
return 0;
}
/*
output:
Array A: 1 2 3 4 5
Array B: 10 20
Shuffled: 1 10 2 20 3 4 5
*/

More Related Content

Similar to C Program to shuffle a given Aay#include stdio.h #include .pdf

Program of sorting using shell sort #include stdio.h #de.pdf
 Program of sorting using shell sort  #include stdio.h #de.pdf Program of sorting using shell sort  #include stdio.h #de.pdf
Program of sorting using shell sort #include stdio.h #de.pdf
anujmkt
 
Daapracticals 111105084852-phpapp02
Daapracticals 111105084852-phpapp02Daapracticals 111105084852-phpapp02
Daapracticals 111105084852-phpapp02
Er Ritu Aggarwal
 
Answers+of+C+sample+exam.docx
Answers+of+C+sample+exam.docxAnswers+of+C+sample+exam.docx
Answers+of+C+sample+exam.docx
ismailaboshatra
 
Answers+of+C+sample+exam.docx
Answers+of+C+sample+exam.docxAnswers+of+C+sample+exam.docx
Answers+of+C+sample+exam.docx
ismailaboshatra
 

Similar to C Program to shuffle a given Aay#include stdio.h #include .pdf (20)

Arrays
ArraysArrays
Arrays
 
week-3x
week-3xweek-3x
week-3x
 
C lab programs
C lab programsC lab programs
C lab programs
 
C lab programs
C lab programsC lab programs
C lab programs
 
C++ lab assignment
C++ lab assignmentC++ lab assignment
C++ lab assignment
 
VTU DSA Lab Manual
VTU DSA Lab ManualVTU DSA Lab Manual
VTU DSA Lab Manual
 
week-21x
week-21xweek-21x
week-21x
 
Program of sorting using shell sort #include stdio.h #de.pdf
 Program of sorting using shell sort  #include stdio.h #de.pdf Program of sorting using shell sort  #include stdio.h #de.pdf
Program of sorting using shell sort #include stdio.h #de.pdf
 
Daapracticals 111105084852-phpapp02
Daapracticals 111105084852-phpapp02Daapracticals 111105084852-phpapp02
Daapracticals 111105084852-phpapp02
 
Data struture lab
Data struture labData struture lab
Data struture lab
 
lets play with "c"..!!! :):)
lets play with "c"..!!! :):)lets play with "c"..!!! :):)
lets play with "c"..!!! :):)
 
Nonlinear analysis of braced frame with hinge by hinge method in c programming
Nonlinear analysis of braced frame with hinge by hinge method in c programmingNonlinear analysis of braced frame with hinge by hinge method in c programming
Nonlinear analysis of braced frame with hinge by hinge method in c programming
 
Microsoft Word Hw#1
Microsoft Word   Hw#1Microsoft Word   Hw#1
Microsoft Word Hw#1
 
Qno 1 (e)
Qno 1 (e)Qno 1 (e)
Qno 1 (e)
 
Answers+of+C+sample+exam.docx
Answers+of+C+sample+exam.docxAnswers+of+C+sample+exam.docx
Answers+of+C+sample+exam.docx
 
Answers+of+C+sample+exam.docx
Answers+of+C+sample+exam.docxAnswers+of+C+sample+exam.docx
Answers+of+C+sample+exam.docx
 
An imperative study of c
An imperative study of cAn imperative study of c
An imperative study of c
 
Functions
FunctionsFunctions
Functions
 
using C++ to Write a program to demonstrate an array of 10 elements an.docx
using C++ to Write a program to demonstrate an array of 10 elements an.docxusing C++ to Write a program to demonstrate an array of 10 elements an.docx
using C++ to Write a program to demonstrate an array of 10 elements an.docx
 
DSC program.pdf
DSC program.pdfDSC program.pdf
DSC program.pdf
 

More from amaresh6333

4.1 (a) SoftwoodsThese generally come from the gymnosperm trees wh.pdf
4.1 (a) SoftwoodsThese generally come from the gymnosperm trees wh.pdf4.1 (a) SoftwoodsThese generally come from the gymnosperm trees wh.pdf
4.1 (a) SoftwoodsThese generally come from the gymnosperm trees wh.pdf
amaresh6333
 
10 leading causes of death in the United States In the recent sta.pdf
10 leading causes of death in the United States In the recent sta.pdf10 leading causes of death in the United States In the recent sta.pdf
10 leading causes of death in the United States In the recent sta.pdf
amaresh6333
 
1.Router 1.It is a machine which aheads datagrams over the networks.pdf
1.Router 1.It is a machine which aheads datagrams over the networks.pdf1.Router 1.It is a machine which aheads datagrams over the networks.pdf
1.Router 1.It is a machine which aheads datagrams over the networks.pdf
amaresh6333
 
1) List currently running jobsANS) see currently runningcommand.pdf
1) List currently running jobsANS) see currently runningcommand.pdf1) List currently running jobsANS) see currently runningcommand.pdf
1) List currently running jobsANS) see currently runningcommand.pdf
amaresh6333
 
I would say very many elements exista as monoatom.pdf
                     I would say very many elements exista as monoatom.pdf                     I would say very many elements exista as monoatom.pdf
I would say very many elements exista as monoatom.pdf
amaresh6333
 
The important thing here is to understand the Standard Electrode Pot.pdf
The important thing here is to understand the Standard Electrode Pot.pdfThe important thing here is to understand the Standard Electrode Pot.pdf
The important thing here is to understand the Standard Electrode Pot.pdf
amaresh6333
 
Tested on linux,ubuntu#include stdlib.h #include systypes..pdf
Tested on linux,ubuntu#include stdlib.h #include systypes..pdfTested on linux,ubuntu#include stdlib.h #include systypes..pdf
Tested on linux,ubuntu#include stdlib.h #include systypes..pdf
amaresh6333
 

More from amaresh6333 (20)

a) It is given E is the event of getting 2. We can observe that 2 is.pdf
a) It is given E is the event of getting 2. We can observe that 2 is.pdfa) It is given E is the event of getting 2. We can observe that 2 is.pdf
a) It is given E is the event of getting 2. We can observe that 2 is.pdf
 
4.1 (a) SoftwoodsThese generally come from the gymnosperm trees wh.pdf
4.1 (a) SoftwoodsThese generally come from the gymnosperm trees wh.pdf4.1 (a) SoftwoodsThese generally come from the gymnosperm trees wh.pdf
4.1 (a) SoftwoodsThese generally come from the gymnosperm trees wh.pdf
 
A special type of IP address is the limited broadcast address 255.25.pdf
A special type of IP address is the limited broadcast address 255.25.pdfA special type of IP address is the limited broadcast address 255.25.pdf
A special type of IP address is the limited broadcast address 255.25.pdf
 
10 leading causes of death in the United States In the recent sta.pdf
10 leading causes of death in the United States In the recent sta.pdf10 leading causes of death in the United States In the recent sta.pdf
10 leading causes of death in the United States In the recent sta.pdf
 
1.Router 1.It is a machine which aheads datagrams over the networks.pdf
1.Router 1.It is a machine which aheads datagrams over the networks.pdf1.Router 1.It is a machine which aheads datagrams over the networks.pdf
1.Router 1.It is a machine which aheads datagrams over the networks.pdf
 
1) List currently running jobsANS) see currently runningcommand.pdf
1) List currently running jobsANS) see currently runningcommand.pdf1) List currently running jobsANS) see currently runningcommand.pdf
1) List currently running jobsANS) see currently runningcommand.pdf
 
Step1 Total pressure = partial pressure of Hydrog.pdf
                     Step1 Total pressure = partial pressure of Hydrog.pdf                     Step1 Total pressure = partial pressure of Hydrog.pdf
Step1 Total pressure = partial pressure of Hydrog.pdf
 
I would say very many elements exista as monoatom.pdf
                     I would say very many elements exista as monoatom.pdf                     I would say very many elements exista as monoatom.pdf
I would say very many elements exista as monoatom.pdf
 
E is simply hexane...no polarity at all...nothing.pdf
                     E is simply hexane...no polarity at all...nothing.pdf                     E is simply hexane...no polarity at all...nothing.pdf
E is simply hexane...no polarity at all...nothing.pdf
 
Ferroxyl contains the chemical compound Potassium.pdf
                     Ferroxyl contains the chemical compound Potassium.pdf                     Ferroxyl contains the chemical compound Potassium.pdf
Ferroxyl contains the chemical compound Potassium.pdf
 
There are two different types of mechanisms. Ther.pdf
                     There are two different types of mechanisms. Ther.pdf                     There are two different types of mechanisms. Ther.pdf
There are two different types of mechanisms. Ther.pdf
 
What must be true about a set of data when its standard deviation is.pdf
What must be true about a set of data when its standard deviation is.pdfWhat must be true about a set of data when its standard deviation is.pdf
What must be true about a set of data when its standard deviation is.pdf
 
When one drives from sea level to trailhead in high Sierras, altitud.pdf
When one drives from sea level to trailhead in high Sierras, altitud.pdfWhen one drives from sea level to trailhead in high Sierras, altitud.pdf
When one drives from sea level to trailhead in high Sierras, altitud.pdf
 
the reaction as follows- 14N7 + 0n1 -- 4He2 + 1.pdf
                     the reaction as follows- 14N7 + 0n1 -- 4He2 + 1.pdf                     the reaction as follows- 14N7 + 0n1 -- 4He2 + 1.pdf
the reaction as follows- 14N7 + 0n1 -- 4He2 + 1.pdf
 
The important thing here is to understand the Standard Electrode Pot.pdf
The important thing here is to understand the Standard Electrode Pot.pdfThe important thing here is to understand the Standard Electrode Pot.pdf
The important thing here is to understand the Standard Electrode Pot.pdf
 
The pathway would be overactive.The GTPase activity of the alpha s.pdf
The pathway would be overactive.The GTPase activity of the alpha s.pdfThe pathway would be overactive.The GTPase activity of the alpha s.pdf
The pathway would be overactive.The GTPase activity of the alpha s.pdf
 
The metal ion is Pb2+.The white precipitate PbCl2 is insoluble in .pdf
The metal ion is Pb2+.The white precipitate PbCl2 is insoluble in .pdfThe metal ion is Pb2+.The white precipitate PbCl2 is insoluble in .pdf
The metal ion is Pb2+.The white precipitate PbCl2 is insoluble in .pdf
 
The electron carriers that feed into the ETC are NADH and FADH2.NA.pdf
The electron carriers that feed into the ETC are NADH and FADH2.NA.pdfThe electron carriers that feed into the ETC are NADH and FADH2.NA.pdf
The electron carriers that feed into the ETC are NADH and FADH2.NA.pdf
 
The developmental potential, or potency of a cell, describes the ran.pdf
The developmental potential, or potency of a cell, describes the ran.pdfThe developmental potential, or potency of a cell, describes the ran.pdf
The developmental potential, or potency of a cell, describes the ran.pdf
 
Tested on linux,ubuntu#include stdlib.h #include systypes..pdf
Tested on linux,ubuntu#include stdlib.h #include systypes..pdfTested on linux,ubuntu#include stdlib.h #include systypes..pdf
Tested on linux,ubuntu#include stdlib.h #include systypes..pdf
 

Recently uploaded

MuleSoft Integration with AWS Textract | Calling AWS Textract API |AWS - Clou...
MuleSoft Integration with AWS Textract | Calling AWS Textract API |AWS - Clou...MuleSoft Integration with AWS Textract | Calling AWS Textract API |AWS - Clou...
MuleSoft Integration with AWS Textract | Calling AWS Textract API |AWS - Clou...
MysoreMuleSoftMeetup
 
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
EADTU
 
SURVEY I created for uni project research
SURVEY I created for uni project researchSURVEY I created for uni project research
SURVEY I created for uni project research
CaitlinCummins3
 

Recently uploaded (20)

PSYPACT- Practicing Over State Lines May 2024.pptx
PSYPACT- Practicing Over State Lines May 2024.pptxPSYPACT- Practicing Over State Lines May 2024.pptx
PSYPACT- Practicing Over State Lines May 2024.pptx
 
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdfFICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
 
MuleSoft Integration with AWS Textract | Calling AWS Textract API |AWS - Clou...
MuleSoft Integration with AWS Textract | Calling AWS Textract API |AWS - Clou...MuleSoft Integration with AWS Textract | Calling AWS Textract API |AWS - Clou...
MuleSoft Integration with AWS Textract | Calling AWS Textract API |AWS - Clou...
 
Spring gala 2024 photo slideshow - Celebrating School-Community Partnerships
Spring gala 2024 photo slideshow - Celebrating School-Community PartnershipsSpring gala 2024 photo slideshow - Celebrating School-Community Partnerships
Spring gala 2024 photo slideshow - Celebrating School-Community Partnerships
 
How to Send Pro Forma Invoice to Your Customers in Odoo 17
How to Send Pro Forma Invoice to Your Customers in Odoo 17How to Send Pro Forma Invoice to Your Customers in Odoo 17
How to Send Pro Forma Invoice to Your Customers in Odoo 17
 
ESSENTIAL of (CS/IT/IS) class 07 (Networks)
ESSENTIAL of (CS/IT/IS) class 07 (Networks)ESSENTIAL of (CS/IT/IS) class 07 (Networks)
ESSENTIAL of (CS/IT/IS) class 07 (Networks)
 
UChicago CMSC 23320 - The Best Commit Messages of 2024
UChicago CMSC 23320 - The Best Commit Messages of 2024UChicago CMSC 23320 - The Best Commit Messages of 2024
UChicago CMSC 23320 - The Best Commit Messages of 2024
 
When Quality Assurance Meets Innovation in Higher Education - Report launch w...
When Quality Assurance Meets Innovation in Higher Education - Report launch w...When Quality Assurance Meets Innovation in Higher Education - Report launch w...
When Quality Assurance Meets Innovation in Higher Education - Report launch w...
 
8 Tips for Effective Working Capital Management
8 Tips for Effective Working Capital Management8 Tips for Effective Working Capital Management
8 Tips for Effective Working Capital Management
 
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
 
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjj
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjjStl Algorithms in C++ jjjjjjjjjjjjjjjjjj
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjj
 
Trauma-Informed Leadership - Five Practical Principles
Trauma-Informed Leadership - Five Practical PrinciplesTrauma-Informed Leadership - Five Practical Principles
Trauma-Informed Leadership - Five Practical Principles
 
The Story of Village Palampur Class 9 Free Study Material PDF
The Story of Village Palampur Class 9 Free Study Material PDFThe Story of Village Palampur Class 9 Free Study Material PDF
The Story of Village Palampur Class 9 Free Study Material PDF
 
Analyzing and resolving a communication crisis in Dhaka textiles LTD.pptx
Analyzing and resolving a communication crisis in Dhaka textiles LTD.pptxAnalyzing and resolving a communication crisis in Dhaka textiles LTD.pptx
Analyzing and resolving a communication crisis in Dhaka textiles LTD.pptx
 
ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...
ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...
ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...
 
Mattingly "AI & Prompt Design: Named Entity Recognition"
Mattingly "AI & Prompt Design: Named Entity Recognition"Mattingly "AI & Prompt Design: Named Entity Recognition"
Mattingly "AI & Prompt Design: Named Entity Recognition"
 
The Liver & Gallbladder (Anatomy & Physiology).pptx
The Liver &  Gallbladder (Anatomy & Physiology).pptxThe Liver &  Gallbladder (Anatomy & Physiology).pptx
The Liver & Gallbladder (Anatomy & Physiology).pptx
 
DEMONSTRATION LESSON IN ENGLISH 4 MATATAG CURRICULUM
DEMONSTRATION LESSON IN ENGLISH 4 MATATAG CURRICULUMDEMONSTRATION LESSON IN ENGLISH 4 MATATAG CURRICULUM
DEMONSTRATION LESSON IN ENGLISH 4 MATATAG CURRICULUM
 
SURVEY I created for uni project research
SURVEY I created for uni project researchSURVEY I created for uni project research
SURVEY I created for uni project research
 
Mattingly "AI and Prompt Design: LLMs with NER"
Mattingly "AI and Prompt Design: LLMs with NER"Mattingly "AI and Prompt Design: LLMs with NER"
Mattingly "AI and Prompt Design: LLMs with NER"
 

C Program to shuffle a given Aay#include stdio.h #include .pdf

  • 1. // C Program to shuffle a given Aay #include #include #include // A utility function to print an Aay void printArray (int A[], int n) { int i; for (i = 0; i < n; i++) printf("%d ", A[i]); printf(" "); } int* shuffle(int A[], int B[]) { int n1 = 5; int n2 = 2; int i = 0, j = 0, k = 0; int *C; C = (int*)malloc(7 * sizeof(int)); // Merging starts while (i < n1 && j < n2) { if (k%2 == 0) { C[k] = A[i]; i++; k++; } else { C[k] = B[j]; k++; j++; }
  • 2. } /* elements in array 'A' are still remaining where as the array 'B' is exhausted */ while (i < n1) { C[k] = A[i]; i++; k++; } /* elements in array 'B' are still remaining where as the array 'A' is exhausted */ while (j < n2) { C[k] = B[j]; k++; j++; } return C; } int main() { int A[] = {1, 2, 3, 4, 5}; int B[] = {10, 20}; int n1 = 5; int n2 = 2; int nC = 7; int *C; C = (int*)malloc(7 * sizeof(int)); printf("Array A: "); printArray(A, n1); printf("Array B: "); printArray(B, n2);
  • 3. C = shuffle (A, B); printf(" Shuffled: "); printArray(C,nC); return 0; } /* output: Array A: 1 2 3 4 5 Array B: 10 20 Shuffled: 1 10 2 20 3 4 5 */ Solution // C Program to shuffle a given Aay #include #include #include // A utility function to print an Aay void printArray (int A[], int n) { int i; for (i = 0; i < n; i++) printf("%d ", A[i]); printf(" "); } int* shuffle(int A[], int B[]) { int n1 = 5; int n2 = 2; int i = 0, j = 0, k = 0; int *C;
  • 4. C = (int*)malloc(7 * sizeof(int)); // Merging starts while (i < n1 && j < n2) { if (k%2 == 0) { C[k] = A[i]; i++; k++; } else { C[k] = B[j]; k++; j++; } } /* elements in array 'A' are still remaining where as the array 'B' is exhausted */ while (i < n1) { C[k] = A[i]; i++; k++; } /* elements in array 'B' are still remaining where as the array 'A' is exhausted */ while (j < n2) { C[k] = B[j]; k++; j++;
  • 5. } return C; } int main() { int A[] = {1, 2, 3, 4, 5}; int B[] = {10, 20}; int n1 = 5; int n2 = 2; int nC = 7; int *C; C = (int*)malloc(7 * sizeof(int)); printf("Array A: "); printArray(A, n1); printf("Array B: "); printArray(B, n2); C = shuffle (A, B); printf(" Shuffled: "); printArray(C,nC); return 0; } /* output: Array A: 1 2 3 4 5 Array B: 10 20 Shuffled: 1 10 2 20 3 4 5 */