int main()
{
cout << "The maximum number of elements in an array is 100. " << endl;
const int MAX = 100;
double myarray[MAX];
int arraysize = Fill_Array(myarray, MAX);
Show_Array(myarray, arraysize);
Reverse_Array(myarray, arraysize);
Show_Array(myarray, arraysize);
Reverse_Positions(myarray, arraysize);
Show_Array(myarray, arraysize);
return 0;
}
int Fill_Array(double ar[], int size)
{
int i = 0;
cout << "\nEnter a double value (Enter q to quit): ";
while (cin >> ar[i] && i < size)
{
i++;
cout << "Enter next value: ";
}
return i;
}
void Show_Array(const double ar[], int size)
{
cout << "\nHere is your array: " << endl << endl;
for (int i = 0; i < size; ++i)
cout <<"\#"<<i+1<<" is "<<ar[i] << endl;
}
void Reverse_Array(double ar[], int size)
{
cout << "Now we reverse the whole array: " << endl;
for (int i = 0; i != size / 2; ++i)
{
double temp;
temp = ar[i];
ar[i] = ar[size - 1 - i];
ar[size - 1 - i] = temp;
}
}
void Reverse_Positions(double ar[], int size)
{
cout << "Enter two positions: " << endl;
int a, b;
if (cin >> a >> b)
{
double temp = ar[b - 1];
ar[b - 1] = ar[a - 1];
ar[a - 1] = temp;
}
}
Something wrong with reverse_position function: It only shows "Enter two positions" and then the function terminates. There is no chance for me to enter the integers a and b. I doubt there is something wrong with the input queue, but I am not sure.
Related
I want the program to ask the user to enter a number and then that function in the array will display on the screen
e.g
case 0 = displayNums (displays numbers entered by the user)
case 2 = getAverage (gets average of numbers entered)
I tried to code the menu to do that, but it only shows up. Nothing happens when the number for the specific function is entered.
#include <iostream>
#define integer 12
int ShowMenu(void);
double displayNums(double[], int);
double GetTotal(double[], int);
double getAverage(double[], int);
double getLargest(double[], int, int*);
double getSmallest(double[], int, int*);
int getNumOccurence(double[], int, int n);
double scaleUp(double[], int);
using namespace std;
int main() {
cout << "enter 12 integers 1 by 1:\n";
int data;
int n = 1;
// array to hold the integers
double arr[integer];
// get the integers
for (int i = 0; i < 12; i++) {
cin >> arr[i];
}
int option;
do {
option = ShowMenu();
switch (option) {
case 0:
double displayNums();
break;
case 1:
double GetTotal();
break;
case 2:
double getAverage();
break;
case 3:
double getLargest();
break;
case 4:
double getSmallest();
break;
case 5:
int getNumOccurence();
break;
case 6:
double scaleUp();
break;
case 7:
break;
default:
cout << "invalid option\n";
}
} while (option != 7);
// displays numbers entered by user
cout << displayNums(arr, integer) << endl;
// displays sum of numbers entered
cout << GetTotal(arr, integer) << endl;
// displays the average
cout << "Average integer is:" << getAverage(arr, integer) << endl;
// displays the largest
cout << "Largest integer is: " << getLargest(arr, integer, &data) << endl;
// display the smallest integer
cout << "Smallest integer is:" << getSmallest(arr, integer, &data) << endl;
// display the occurence of the num
cout << "Occurence integer is:" << getNumOccurence(arr, integer, n) << endl;
// display the scale up integers
cout << "Scaled up integers are:" << scaleUp(arr, integer) << endl;
return 0;
}
int ShowMenu(void) {
int option;
cout << "\t0. Display Numbers\n";
cout << "\t1. Get Total of numbers\n";
cout << "\t2. Get Average\n";
cout << "\t3. Get Largest\n";
cout << "\t4. Get Smallest\n";
cout << "\t5. Get Number Occurences\n";
cout << "\t6. Scale Up\n";
cout << "\t7. Quit\n";
cout << "\t\t\tOption ? ";
cin >> option;
return option;
}
double displayNums(double arr[], int size) {
double display = 0;
for (int i = 0; i < size; i++) { // for loop
}
cout << "the numbers that you have entered into the array are:" << endl;
for (int i = 0; i < size; i++) {
cout << arr[i] << endl; // displays numbers entered
}
return display;
}
double GetTotal(double arr[], int size) {
double sum = 0;
for (int i = 0; i < size; i++) {
}
cout << "" << endl;
for (int i = 0; i < 12; i++) {
sum += arr[i];
}
cout << "The sum of all numbers entered is:" << sum << endl;
return sum;
}
double getAverage(double arr[], int size) {
double sum = 0.0, avg;
for (int i = 0; i < size; i++) {
sum = sum + arr[i];
}
avg = sum / size;
return avg;
}
double getLargest(double arr[], int size, int* data) {
double large = 0;
for (int i = 0; i < size; i++) {
if (arr[i] > large) {
large = arr[i];
*data = i + 1;
}
}
return large;
}
double getSmallest(double arr[], int size, int* data) {
double small = 10000;
for (int i = 0; i < size; i++) {
if (arr[i] < small) {
small = arr[i];
*data = i + 1;
}
}
return small;
}
int getNumOccurence(double arr[], int size, int n) {
// only works when you insert repetable number 1
int count = 0;
for (int i = 0; i < size; i++) {
if (n == arr[i]) {
count++;
}
}
return count;
}
double scaleUp(double arr[], int size) {
int factor = 0;
cout << "enter the scale up factor";
cin >> factor;
for (int i = 0; i < size; i++) {
arr[i] *= factor;
cout << arr[i] << endl;
}
return factor;
}
You could use a std::map instead of all those cases.
// Declare a synonym for a function pointer
typedef double (*Function_Pointer)();
// Declare an abbreviation for the map:
typedef std::map<int, Function_Pointer> Function_Display_Table;
// Initialize the function table:
Function_Display_Table display_table;
display_table[0] = display_nums;
display_table[1] = GetTotal;
display_table[2] = getAverage();
//...
// To process your selection:
Function_Pointer fp = display_table.at(selection);
double return_value = fp();
To expand your menu selection, you only have to add rows to the display_table.
(Sorry if this is formatted terribly. I've never posted before.)
I've been working on a program for class for a few hours and I can't figure out what I need to do to my function to get it to do what I want. The end result should be that addUnique will add unique inputs to a list of its own.
#include <iostream>
using namespace std;
void addUnique(int a[], int u[], int count, int &uCount);
void printInitial(int a[], int count);
void printUnique(int u[], int uCount);
int main() {
//initial input
int a[25];
//unique input
int u[25];
//initial count
int count = 0;
//unique count
int uCount = 0;
//user input
int input;
cout << "Number Reader" << endl;
cout << "Reads back the numbers you enter and tells you the unique entries" << endl;
cout << "Enter 25 positive numbers. Enter '-1' to stop." << endl;
cout << "-------------" << endl;
do {
cout << "Please enter a positive number: ";
cin >> input;
if (input != -1) {
a[count++] = input;
addUnique(a, u, count, uCount);
}
} while (input != -1 && count < 25);
printInitial(a, count);
printUnique(u, uCount);
cout << "You entered " << count << " numbers, " << uCount << " unique." << endl;
cout << "Have a nice day!" << endl;
}
void addUnique(int a[], int u[], int count, int &uCount) {
int index = 0;
for (int i = 0; i < count; i++) {
while (index < count) {
if (u[uCount] != a[i]) {
u[uCount++] = a[i];
}
index++;
}
}
}
void printInitial(int a[], int count) {
int lastNumber = a[count - 1];
cout << "The numbers you entered are: ";
for (int i = 0; i < count - 1; i++) {
cout << a[i] << ", ";
}
cout << lastNumber << "." << endl;
}
void printUnique(int u[], int uCount) {
int lastNumber = u[uCount - 1];
cout << "The unique numbers are: ";
for (int i = 0; i < uCount - 1; i++) {
cout << u[i] << ", ";
}
cout << lastNumber << "." << endl;
}
The problem is my addUnique function. I've written it before as a for loop that looks like this:
for (int i = 0; i < count; i++){
if (u[i] != a[i]{
u[i] = a[i]
uCount++;
}
}
I get why this doesn't work: u is an empty array so comparing a and u at the same spot will always result in the addition of the value at i to u. What I need, is for this function to scan all of a before deciding whether or no it is a unique value that should be added to u.
If someone could point me in the right direction, it would be much appreciated.
Your check for uniqueness is wrong... As is your defintion of addUnique.
void addUnique(int value, int u[], int &uCount)
{
for (int i = 0; i < uCount; i++){
if (u[i] == value)
return; // already there, nothing to do.
}
u[uCount++] = value;
}
Count all the records from the file "8.dat". To read each individual recording perform dynamic memory capture.
Sort the records to different keys:
Item number (ascending);
The cost (descending);
Number of stock (descending).
Use selection sort
Total sorting will be done 12 times, each time the array is sorted in its original condition.
For each case count of comparisons to and permutations.
Below code implements insertion sort. Twice, without saying so much.
I need to use selection sort. How to do selection sort?
#include <iostream>
#include <fstream>
#include <conio.h>
using namespace std;
struct PRICE
{
int number;
char name[20];
int cost;
int quantity;
} *pm;
int Menu();
void PrintPRICE(PRICE);
void sort_cost(PRICE*, int);
void sort_quantity(PRICE*, int);
long file_size(const char*);
int main()
{
int count = 0;
const char *fname = "D:\8.dat";
FILE* file = fopen(fname, "r");
if (file != NULL)
{
long size = file_size(fname);
count = size / sizeof PRICE;
pm = new PRICE[count];
fread(pm, sizeof PRICE, count, file);
fclose(file);
}
for (int i=0; i<count; i++)
{
PrintPRICE(pm[i]);
cout << endl;
}
int ch = Menu();
switch (ch)
{
case 1:
{
sort_cost(pm, count);
cout << endl;
cout << " Result: " << endl;
cout << "-----------------------" << endl;
for (int i=0; i<count; i++)
{
PrintPRICE(pm[i]);
cout << endl;
}
break;
}
case 2:
{
sort_quantity(pm, count);
cout << " Result: " << endl;
cout << "-----------------------" << endl;
for (int i=0; i<count; i++)
{
PrintPRICE(pm[i]);
cout << endl;
}
break;
}
default: break;
}
delete [] pm;
_getch();
}
void PrintPRICE(PRICE price)
{
cout << " Product: " << price.name << endl;
cout << " Number of orden: " << price.number << endl;
cout << " Product cost: " << price.cost << endl;
cout << " Quantity in stock: " << price.quantity << endl;
cout << "-----------------------------------n" << endl;
}
long file_size(const char* filename)
{
FILE *Pfile = NULL;
Pfile = fopen(filename, "rb");
fseek(Pfile, 0, SEEK_END);
long size = ftell(Pfile);
fclose(Pfile);
return size;
}
void sort_cost(PRICE* array, int count)
{
int change = 0;
int comparesion = 0;
for (int i=1; i<count; i++)
{
PRICE key = array[i];
int j = i - 1;
comparesion++;
while (i>=0 && array[i].cost>key.cost)
{
array[j + 1] = array[j];
j = j - 1;
change++;
}
array[j + 1] = key;
}
cout << "n Quantity change: " << change << endl;
cout << " Quantity comparesion: " << comparesion << endl;
}
void sort_quantity(PRICE* array, int count)
{
int change = 0;
int comparesion = 0;
for (int i=1; i<count; i++)
{
PRICE key = array[i];
int j = i - 1;
comparesion++;
while (j>=0 && array[i].quantity>key.quantity)
{
array[j + 1] = array[j];
j = j - 1;
change++;
}
array[j + 1] = array[j];
}
cout << "n Quantity change: " << change << endl;
cout << " Quantity comparesion: " << comparesion << endl;
}
int Menu()
{
int n;
cout << " 1 - Sort by cost" << endl;
cout << " 2 - Sort by quantity" << endl;
cout << "n Your choice: "; cin >> n;
return n;
}
source code for the selection sort
void selectSort(int arr[], int n)
{
int pos_min,temp;
for (int i=0; i < n-1; i++)
{
pos_min = i;
for (int j=i+1; j < n; j++)
{
if (arr[j] < arr[pos_min])
pos_min=j;
}
if (pos_min != i)
{
temp = arr[i];
arr[i] = arr[pos_min];
arr[pos_min] = temp;
}
}
}
#include <iostream>
#include <iomanip>
using namespace std;
// Function Prototype
void sortArray(int array[], int size);
void showArray(const int arr[], int size);
void average(int testScores[], int size);
int main()
{
int *testScores;
int numGrades,count;
cout << "How many grades? " << endl;
cin >> numGrades;
testScores = new int[numGrades];
cout << "Please enter the scores below." << endl;
for (count = 0; count < numGrades; count++)
{
cin >> testScores[count];
}
sortArray(testScores, numGrades);
showArray(testScores, numGrades);
average(testScores, numGrades);
delete[] testScores;
testScores = 0;
system("pause");
return (0);
}
//function for ascending order
int * testScores[];
void sortArray(int array[], int size)
{
bool swap;
int temp;
do
{
swap = false;
for (int count = 0; count < (size - 1); count++)
{
if (array[count]> array[count + 1])
{
temp = array[count];
array[count] = array[count + 1];
array[count + 1] = temp;
swap = true;
}
}
} while (swap);
}
// display array function
void showArray(const int arr[], int size)
{
cout << " Scores in ascending order." << endl;
for (int count = 0; count < size; count++)
{
cout << " " << arr[count] << "";
}
cout << endl;
cout << endl;
}
// function to get average of the array
void average(int testScores[], int numGrades)
{
float total = 0.0;
for (int count = 0; count < numGrades; count++)
{
total += testScores[count];
}
float average = total / numGrades;
cout << " This is the average of the scores entered." << endl;
cout << endl;
cout << fixed << showpoint << setprecision(2);
cout << " *** " << average << " ***" << endl;
}
So this program was made to allow students to enter as many test scores as they want and the program will show them in ascending order and then will calculate the average of all test scores. It works wonderful until you enter a negative test score and then it throws the averaging process off. I can't seem to be able to make the program not accept the negative numbers. Any clues or hints would work wonders! Please and Thank You.
You are doing this:-
cin >> testScores[count];
This would accept all integers positive as well as negative. One way is to check the number, if it's positive then insert it into array.
So for my problem I need to have a dynamically allocated array that is to be created in the main function and populated in another function. The issue I'm having is that I then need to use that array in other functions and my array has no value after I populate it in my function (or at least this seems to be the case) Here is my code:
#include <iostream>
#include <iomanip>
using namespace std;
//prototypes
int getNumber();
void getMovieData(int *ptrToArray, int arraySize);
void sort(int *ptrToArray, int arraySize);
double getAverage(int *ptrToArray, int arraySize);
void print(int *ptrToArray, int arraySize);
int main()
{
int stuNum = 0;
int* stuArray;
stuArray = new int[stuNum];
getMovieData(stuArray, stuNum);
cout << "--- Here is the data you entered ---" << endl;
print(stuArray, stuNum);
sort(stuArray, stuNum);
cout << "--- Here is the data you entered sorted ---" << endl;
print(stuArray, stuNum);
cout << fixed << setprecision(2);
cout << "Here is the average of your survey" << getAverage(stuArray, stuNum) << endl;
system("pause");
return 0;
}
int getNumber()
{
int userNum;
cin >> userNum;
while (userNum <= 0)
{
cout << "Error number must be greater than zero." << endl;
cin >> userNum;
}
return userNum;
}
void getMovieData(int *ptrToArray, int arraySize)
{
cout << "Enter the number of students being surveyed: ";
arraySize = getNumber();
for (int i = 0; i < arraySize; i++)
{
cout << "Enter the movies seen by Student " << i + 1 << ": ";
ptrToArray[i] = getNumber();
}
return;
}
void sort(int *ptrToArray, int arraySize)
{
for (int i = 0; i < arraySize; i++)
{
for (int j = 0; j < arraySize - 1; j++)
{
if (ptrToArray[j] > ptrToArray[j + 1])
{
int temp = ptrToArray[j];
ptrToArray[j] = ptrToArray[j + 1];
ptrToArray[j + 1] = temp;
}
}
}
}
double getAverage(int *ptrToArray, int arraySize)
{
int total = 0;
for (int i = 0; i < arraySize; i++) { total = total + ptrToArray[i]; }
return total;
}
void print(int *ptrToArray, int arraySize)
{
for (int i = 0; i < arraySize; i++) { cout << ptrToArray[i] << "\t"; }
cout << endl;
}
You are allocating an array with zero elements. Change the value of stuNum to a positive number representing the number ints you need.