Question

C++ Problems: 1. Write a program that reads in an array of user given size n and then it reads se...

C++ Problems:

1. Write a program that reads in an array of user given size n and then it reads several increment values terminated by 0. The program calls each time a function incarray() that accepts an array A, its size S and an increment value inc and updates yje array by incrementing all its values by the passed value of inc. The program prints out the array after each update.

2. write a program that reads in a n array of user given size N and then sorts the array either in ascending or descending order depending on a user defined character input AD (AD='A' for ascending and AD='D' for descending)

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Question 1:

Here is code:

#include <iostream>

using namespace std;

int *incarray(int *arr, int newEle, int size)

{

int *newArr = (int *)malloc(sizeof(int) * (size + 1));

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

{

newArr[i] = arr[i];

}

newArr[size] = newEle;

return newArr;

}


int main()

{

int *array, num, size = 0;

while (true)

{

cout << "Enter number : ";

cin >> num;

if (num == 0)

break;

array = incarray(array, num, size);

size++;

}

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

{

cout << array[i] << " ";

}

cout << endl;

return 0;

}

Output:

Enter number :2 Enter number 3 Enter number 4 Enter number 6 Enter number:7 Enter number 8 Enter number 9 Enter number e 2 3

Question 2:

Here is code:

#include <iostream>

using namespace std;


void sortArray(int *array, int size, char type)

{

int i, j, temp;

if (type == 'A')

{

for (i = 0; i < size; i++)

{

for (j = i + 1; j < size; j++)

{

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

{

temp = array[i];

array[i] = array[j];

array[j] = temp;

}

}

}

}

if (type == 'D')

{

for (i = 0; i < size; i++)

{

for (j = i + 1; j < size; j++)

{

if (array[i] < array[j])

{

temp = array[i];

array[i] = array[j];

array[j] = temp;

}

}

}

}

}

int main()

{

int *array, num, size = 0;

while (true)

{

cout << "Enter number : ";

cin >> num;

if (num == 0)

break;

array = incarray(array, num, size);

size++;

}

sortArray(array, size, 'D');

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

{

cout << array[i] << " ";

}

cout << endl;

return 0;

}

Output:

Enter number : 5 Enter number 6 Enter number : 1 Enter number :2 Enter number :9 Enter number : 7 987654 21

Add a comment
Know the answer?
Add Answer to:
C++ Problems: 1. Write a program that reads in an array of user given size n and then it reads se...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • LANGUAGE: JAVA Sorting an array: 2. Write a program that asks the user for 5 numbers...

    LANGUAGE: JAVA Sorting an array: 2. Write a program that asks the user for 5 numbers and stores them in an array called data. Then call a method that accepts the array as an argument, sorts the array in ascending order and prints out the original AND sorted array (Hint: The method does not return anything in this case, so it should be set to void).

  • use C++ to write the following program. needs to ask the user for n The user...

    use C++ to write the following program. needs to ask the user for n The user must put in those 5 values, probably using a for loop. NOTE: You can assume the number of values to be entered is 5. My attempted program below: (not correct, but just a basis) 4. Larger Than n In a program, write a function that accepts three arguments: an array, the size of the array, and a number n. Assume that the array contains...

  • Q#1 Write a C++ program that reads n integer values and stores them in an array...

    Q#1 Write a C++ program that reads n integer values and stores them in an array of maximum 20 integers. Read from the user a valid value of n (n should not exceed 20). After reading the n array elements find the maximum and minimum elements.

  • (C++) Write a function that accepts an int array and the array’s size as arguments. The function...

    (C++)Write a function that accepts an int array and the array’s size as arguments.The function should create a new array that is twice the size of the argument array.The function should copy the contents of the argument array to the new array, and initialize the unused elements of the second array with 0.The function should return a pointer to the new array.Demonstrate the function by using it in a main program that reads an integer N (that is not more...

  • Write a program that first reads an integer for the array size, then reads double values...

    Write a program that first reads an integer for the array size, then reads double values into the array, and displays the number of values greater than the average. Enter array size: 6 Enter a number: 1.1 Enter a number: 2.2 Enter a number: 3.4 Enter a number: 3.9 Enter a number: 5.5 Enter a number: 8.75 The number of values greater than the average is 2

  • 9.10: Reverse Array Write a function that accepts an int array and the array’s size as arguments

    9.10: Reverse Array Write a function that accepts an int array and the array’s size as arguments. The function should create a copy of the array, except that the element values should be reversed in the copy. The function should return a pointer to the new array. Demonstrate the function by using it in the main program that reads an integer N  (that is not more than 50) from standard input and then reads N  integers from a file named...

  • *c language* Write a program that takes two sorted array A,B of size m,n respectively where...

    *c language* Write a program that takes two sorted array A,B of size m,n respectively where A is sorted in the ascending order and B is sorted in the descending order, and merge these two arrays into a sorted array C.   For example if A=[1, 4, 5, 7, 8] and B=[10,9,8,6,4,2]   then C=[1,2,4,4,5,6,7,8,8,9,10].

  • Please do in C and only use stdio.h. Write a program that reads n integers into...

    Please do in C and only use stdio.h. Write a program that reads n integers into an array, then prints on a separate line the value of each distinct element along with the number of times it occurs. The values should be printed in ascending order. Turn in your code and input/result together. Suppose, for example, that you input the values -7 3 3 -7 5 5 3 as the elements of your array. Then your program should print -7...

  • c++ no pointers just follow instructions Write a program (array.cpp) that read positive floating point numbers...

    c++ no pointers just follow instructions Write a program (array.cpp) that read positive floating point numbers into an array of capacity 100. The program will then ask for one of three letters(A, M or S). if the user inputs something other than one of these letters, then the program should ask for that input until an A, M, or S is entered. A do-while loop should be used for this. If the user inputs an A, then the program should...

  • Write a C Program that inputs an array of integers from the user along-with the length...

    Write a C Program that inputs an array of integers from the user along-with the length of array. The program then prints out the array of integers in reverse. (You do not need to change (re-assign) the elements in the array, only print the array in reverse.) The program uses a function call with array passed as call by reference. Part of the Program has been given here. You need to complete the Program by writing the function. #include<stdio.h> void...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT