Click here to Skip to main content
15,896,606 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Given an array of numbers, containing 10 elements. Write a function that sorts an array in ascending or descending order, depending on the third function parameter. If it is equal to 1, sort is carried out in a descending order if 0 then it is a ascending order. First two 2 function parameters is an array and its size, the third parameter is default equal to 1.

There is red line under the word "default" and "homework".(

What I have tried:

C++
#include<iostream>
#include<ctime>

using namespace std;



int homework(int arr[], const int SIZE = 10, int default = 1) {

	srand(time(NULL));

	for (int i = 0; i < SIZE; i++)
	{
		arr[i] = rand() % 100;
	}

	for (int i = 0; i < SIZE; i++)
	{
		cout << arr[i] << " ";
	}

	cout << endl << endl;

	if (default == 1) {
		for (int i = 0; i < SIZE; i++)
		{
			for (int i = 0; i < SIZE - 1; i++) {
				if (arr[i] < arr[i + 1]) {
					int temp = arr[i];
					arr[i] = arr[i + 1];
					arr[i + 1] = temp;
				}
			}
		}
	}
	else if (default != 1) {
		for (int i = 0; i < SIZE; i++)
		{
			for (int i = 0; i < SIZE - 1; i++) {
				if (arr[i] > arr[i + 1]) {
					int temp = arr[i];
					arr[i] = arr[i + 1];
					arr[i + 1] = temp;
				}
			}
		}
	}

	for (int i = 0; i < SIZE; i++)
	{
		cout << arr[i] << " ";
	}

	return 0;
}


int main() {

	const int SIZE = 10;

	int arr[SIZE];

	homework(arr, SIZE, 1);
}
Posted
Updated 10-Feb-18 8:54am
v2
Comments
David_Wimbley 10-Feb-18 14:34pm    
So what is your issue? If visual studio "doesn't run it"...what error are you getting?

1 solution

you should change the function head to this:
C++
int homework(int arr[], const int size, bool loopType) 

you cant use default parameters in the implementation. Dont use BIG LETTER as var names, but only for constants. And "default" is an reserverd keyword (in switch loopes)
your coding
C++
if (default == 1) {
		// ...
	}
	else if (default != 1)///NOT NEEDED !!!!
resolves to this
C++
if (loopType) {
		// ...
	}
	else {
you can do it in one loop
C++
for (int i = 0; i < SIZE; i++)
	{
		arr[i] = rand() % 100;// set value
		cout << arr[i] << " "; //read and output value
	}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900