Question

In: Computer Science

Create a program in C that performs the following tasks: Define a macro arraySize of size...

Create a program in C that performs the following tasks:

  • Define a macro arraySize of size n
  • Create two arrays of size n.
  • Pass these arrays to a function fillArrays that randomizes enough integers between 1-100 (inclusive) to fill both arrays.
  • Print both of these arrays.
  • Pass the filled arrays to a second function mergeArrays that creates a third array of size 2n.
  • Still in mergeArrays, store the values from the original two arrays into your new array in reverse.
  • Print out your sorted array.

Solutions

Expert Solution

#include <stdio.h>

#include <stdlib.h>

#include <time.h>

#define ARRAY_SIZE 10

void mergeArrays( int *var1, int *var2)

{

int n1= 2*ARRAY_SIZE; //To double the size of new array

int arrnew[n1];

int x1 = ARRAY_SIZE; //To process 2nd array from newarray's index n, bcz there are already n elements.

int index = 0;

for(int i = ARRAY_SIZE-1 ; i>=0 ;--i){

arrnew[index++] = var2[i];

}

for(int i = ARRAY_SIZE-1 ; i>=0 ;--i){

arrnew[index++] = var1[i];

}


//To sort an array

for (int i = 0; i < n1; ++i)

{

for (int j = i + 1; j < n1; ++j)

{

if (arrnew[i] > arrnew[j])

{

int tempval = arrnew[i];

arrnew[i] = arrnew[j];

arrnew[j] = tempval;

}

}

}


//To print new sorted array

printf("\nValues in new sorted array are:");

for(int x=0; x<n1; x++)

{

printf("%d ", arrnew[x]);

}

printf("\n");

printf("\n");


}

void fillArray(int arr[]){

for (int i=0;i<ARRAY_SIZE;i++){

//To generate random number within 100

int randno =rand() % 100 + 1; //Includes 1 and 100

arr[i]=randno;

}


}

int main()

{

//Variable declarations

int arr1[ARRAY_SIZE]; //array1

int arr2[ARRAY_SIZE]; //array2

int min,max,randno; //To generate random number

//The srand() function sets its argument as the seed for a new sequence of pseudo-random integers to be returned by rand()

srand (time(NULL) );


fillArray(arr1);

//Fill array with random number

printf("Values in array1 are:");

for(int x=0; x<ARRAY_SIZE; x++)

{

printf("%d ",arr1[x]) ;

}

printf("\n");


fillArray(arr2);

//Fill array with random number

printf("Values in array2 are:");

for(int x=0; x<ARRAY_SIZE; x++)

{

printf("%d ",arr2[x]) ;

}

printf("\n");


//Call merge array fuction to merge and sort array

mergeArrays(arr1, arr2);

return 0;

}

======================================

SEE OUTPUT

Thanks, PLEASE COMMENT if there is any concern. Please UPVOTE


Related Solutions

You should write a small C++ program that performs the following tasks: First, your program should...
You should write a small C++ program that performs the following tasks: First, your program should read in a set of 5 integers from “standard in” (remember cin). The numbers that you read in will be either zero or positive. If at least one of the five numbers entered is non-zero then your program should calculate the sum of the numbers and report that back to the user using “standard output” (remember cout). Then your program should be ready to...
Create a C program that performs the following (please comment the codes): a) Create a Stack...
Create a C program that performs the following (please comment the codes): a) Create a Stack ADT. Stack should be implemented using the linked list. b) Enter 10 random integer numbers between 0 to 50 in the stack. c) After pushing each element, print the content of the top of the stack. c) Then pop out those 10 integer numbers and print those numbers. d) Finally destroy the Stack.
Create a C++ integer linked list program that performs the following methods below: Please create these...
Create a C++ integer linked list program that performs the following methods below: Please create these three source files: intList.h, intList.cpp, & intListTest.cpp. Implement recursive routines in the intList class to do the following: Print the list in reverse order Return the value in the middle node Return the average of all the odd values Remove every node containing an odd value Modify main (in IntListTest.cpp) so it does the following: Insert the numbers 1, 3, 4, 6, 7, 10,...
Write a program that performs the following two tasks in java Reads an arithmetic expression in...
Write a program that performs the following two tasks in java Reads an arithmetic expression in an infix form, stores it in a queue (infix queue) and converts it to a postfix form (saved in a postfix queue). Evaluates the postfix expression. Use linked lists to implement the Queue and Stack ADTs. DO NOT USE BUILT IN JAVA CLASSES
Modify the GreenvilleRevenue program so that it uses the Contestant class and performs the following tasks:...
Modify the GreenvilleRevenue program so that it uses the Contestant class and performs the following tasks: The program prompts the user for the number of contestants in this year’s competition; the number must be between 0 and 30. The program continues to prompt the user until a valid value is entered. The expected revenue is calculated and displayed. The revenue is $25 per contestant. For example if there were 3 contestants, the expected revenue would be displayed as: Revenue expected...
Write a program that performs the following two tasks: Reads an arithmetic expression in an infix...
Write a program that performs the following two tasks: Reads an arithmetic expression in an infix form, stores it in a queue (infix queue) and converts it to a postfix form (saved in a postfix queue). Evaluates the postfix expression. Use the algorithms described in class/ lecture notes. Use linked lists to implement the Queue and Stack ADTs. Using Java built-in classes will result in 0 points. You must use your own Stack and Queue classes (my code is a...
Consider the following program: 1 #define Size 64 int A[Size; Size], B[Size; Size], C[Size; Size]; int...
Consider the following program: 1 #define Size 64 int A[Size; Size], B[Size; Size], C[Size; Size]; int register i, j; for (j = 0; j< Size; j ++) { { for (i = 0; i< Size; i++) C[i; j] = A[i; j] + B[i; j]; } } Assume that the program is running on a system using demand paging and the page size is 1 Kilobyte. Each integer is 4 bytes long. It is clear that each array requires a 16-page...
In Java: Problem 1. Write a program that performs the following two tasks: 1. Reads an...
In Java: Problem 1. Write a program that performs the following two tasks: 1. Reads an arithmetic expression in an infix form, stores it in a queue (infix queue) and converts it to a postfix form (saved in a postfix queue). 2. Evaluates the postfix expression. Use the algorithms described in class/ lecture notes. Use linked lists to implement the Queue and Stack ADTs. Using Java built-in classes will result in 0 points. You must use your own Stack and...
Create a C++ program based on the following criteria: Define an integer for the user selection...
Create a C++ program based on the following criteria: Define an integer for the user selection called selection Define a double called ticketPrice that is initialized to 20 Create a numeric menu for ticket prices, thus: Adult is 15-64 and is normal price (option 1) Child is 0-14 and is half normal price (option 2) Senior is 65+ and is 0.8x normal price (option 3) Give an option to exit (option 4) Based on their selection, output the price of...
Write a C++ code for these 2 questions 1. Create an integer variable named ‘arraySize’ and...
Write a C++ code for these 2 questions 1. Create an integer variable named ‘arraySize’ and initialize the value to 1000. 2.. Open an output file named ‘GroupAssignment2Results.csv’. The first line of this file should be the column headings: ‘Array Size’, ‘Bubble Sort Time’, ‘Selection Sort Time’, ’Insertion Sort Time’, ‘Quick Sort Time’, ‘Merge Sort Time’.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT