Khubaib Khawar - 1 year ago 83

C++ Question

This is the porgram I am writing for my university's Assignment. I have written all the codes that takes 10 integers from user, and asks user to press 1 to show odd integers in ascending order from the list or 2 to show the even integers in ascending order from the list.

Well, I have declared and defined the Bubble Sorting function before the main() function in program, and used the function later in main() to sort the even and odd numbers in ascending order. But, I am still getting the error that the function was not declared in this scope even when I have declared it at the top. I tried all possible things I could do. Kindly help me what should I do? Below is my code

`#include <iostream>`

#include <conio.h>

using namespace std;

void BuubleSort_Function(int [], int);

void BuubleSort_Function(int arr[], int arrSize)

{

int extraMem;

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

{

for(int arrIndex = 0; arrIndex < arrSize - 1; arrIndex++)

{

if(arr[arrIndex] > arr[arrIndex+1])

{

extraMem = arr[arrIndex];

arr[arrIndex] = arr[arrIndex+1];

arr[arrIndex+1] = extraMem;

}

}

}

}

main()

{

int num[10], i, even[10], odd[10], inputOpt, totalEvens = 0, totalOdds = 0;

system("cls");

cout << "Please enter 10 integers: " << endl;

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

{

cin >> num[i];

}

cout << endl << endl << endl << "1. Show odd numbers in ascending order and their total numbers" << endl;

cout << "2. Show even numbers in ascending order and their total numbers" << endl;

do

{

cout << endl << "Enter 1 for the first option or 2 for the second option: ";

cin >> inputOpt;

if(inputOpt != 1 && inputOpt != 2)

{

cout << "Wrong Input! Please enter the correct input value";

}

}

while(inputOpt != 1 && inputOpt != 2);

if(inputOpt == 1)

{

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

{

if(num[i] % 2 == 1)

{

odd[totalOdds] = num[i];

totalOdds++;

}

}

BubbleSort_Function(odd,totalOdds);

cout << endl << "The total numbers of Odds Integers are " << totalOdds;

cout << endl << "The Integers arranged in Ascending Order:" << endl;

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

{

cout << odd[i] << "\t";

}

}

if(inputOpt == 2)

{

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

{

if(num[i] % 2 == 0)

{

even[totalEvens] = num[i];

totalEvens++;

}

}

BubbleSort_Function(even,totalEvens);

cout << endl << "The total numbers of Odds Integers are " << totalEvens;

cout << endl << "The Integers arranged in Ascending Order:" << endl;

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

{

cout << even[i] << "\t";

}

}

}

Answer Source

A simple typo: the function is declared and implemented as `BuubleSort_Function`

.

You attempt to call it using `BubbleSort_Function`

.

The compiler error messages are very helpful. Do learn to interpret them.

(Finally, standard C++ requires you to mark `main()`

as returning an `int`

- a C++ compiler will insert an implicit `return 0`

into `main`

if it's missing. Some compilers - especially ones for embedded systems - drop this requirement but that is a deviation from the standard.).