I am almost done with my code; however, one part is not working well. Simply, reading from a file that contains only numbers(ex. cars sold). by using an array, trying to get the total of those numbers, max, and the number of the max index. My question is: how can I return the two values from my MaxSold function? it returns only the max added to the index which is not correct. the result should point to the employee number then the max.
This is my code so far:
#include <iostream>
#include <fstream>
/*#include <vector>
#include <iomanip>*/
void Read(int arryList[], int size);
void Print(int arryList[], int size);
int total(int arryList[], int size);
int MaxSold(int arryList[], int size, int& number);
using namespace std;
ifstream inFile("C:\\cars.dat");
int main()
{
int cars[7];
int i;
Read(cars,7);
Print(cars,7);
cout<<"The total of sold cars is: "<<total(cars, 7)<< "\n";
cout<<"The Max "<< MaxSold(cars, 7, i);
}
void Read(int arryList[], int size){
for(int i = 0; i < 7; i++)
{
inFile >> arryList[i];
}
return;
}
void Print(int arryList[], int size){
for (int i = 0; i < 7; i++){
cout << i + 1 << "-"<< arryList[i] << "\n";
}
return ;
}
int total(int arryList[], int size){
int sum = 0;
for (int i = 0; i < size; i++){
sum +=arryList[i];
}
return sum;
}
int MaxSold(int arryList[], int size, int& number){
int Maximum= 0;
int relate=0;
for( int i=0 ; i<7 ; i++){
if (arryList[i] > Maximum){
Maximum = arryList[i];
relate = i+1;
}
}
return Maximum, relate;
}
Use std::pair
#include<utility>
//..
std::pair<int,int> MaxSold(int arryList[], int size, int& number)
{
//...
return std::make_pair( Maximum, relate );
}
Then,
std::pair<int,int> p = MaxSold(cars, 7, i) ;
std::cout<< p.first ; //maximum
std::cout<< p.second ; //relate
You cannot return more than one value from a function. Of course, that value can be a container for multiple values. It can be your own custom type, but the simplest way would be to return a std::pair<int,int>.
std::pair<int, int> MaxSold(int arryList[], int size, int& number)
{
// ...
return std::make_pair(Maximum, relate);
}
Related
I am getting the following error in c++ code
error:
'getmin' was not declared in this scope
cout<<"Minimum value is"<<getmin(num, size)<<endl;
Code//
Int getmin(int num[], int n){
int min= INT32_MAX;
for(int i=0;i<n;i++){
if(num[i] < min){
min=num[I];
}
}
return min;
}
int getmax(int num[], int n){
int max= INT32_MIN;
for(int i=0;i<n;i++){
if(num[i]>max){
max=num[I];
}
}
return max;
}
The problem you made is that you passed two integers inside the getmin(array[], integer) whereas you are supposed to pass an array and an integer. Since you passed integer and integer, the program search for a function with signature getmin(integer, integer) when you call the function in main(). But the function is inexistent; thus get the error'getmin' was not declared in this scope
Solved:
int getmin(int num[], int n)
{
int min= INT32_MAX;
for(int i=0;i<n;i++){
if(num[i] < min){
min=num[i];
}
}
return min;
}
int main()
{
int a[6]={2,7,4,0,9,10};
cout<<getmin(a, 6);
return 0;
}
Not sure that you've prototyped your function getmin().
If not, your every function should be declared before it is used.
int getmin(int num[], int n);
That is before int main()
Here's my modified code,
#include <iostream>
using namespace std;
int getmin(int num[], int n);
int getmax(int num[], int n);
int main()
{
int num[10] = {10,20,30,40,50,60,40,5};
int n = 7;
cout << "Minimum value is:" << getmin(num, n) << endl;
cout << "Minimum value is: " << getmax(num, n) << endl;
}
int getmin(int num[], int n)
{
int min = INT32_MAX;
for (int i = 0; i <= n; i++)
{
if (num[i] < min)
{
min = num[i];
}
}
return min;
}
int getmax(int num[], int n) {
int max = INT32_MIN;
for (int i = 0; i < n; i++) {
if (num[i] >= max) {
max = num[i];
}
}
return max;
}
I tried to write a simple code to calculate an array elements' sum. every thing looks normal but the function return the sum value wrongly (it always multiply it by two). Although if I want just print the value, it works fine.
this is the code:
#include <iostream>
using namespace std;
void getElements(int[],int);
int sumOfElements(int[],int);
int number;
int sum=0;
int main()
{
int a[10];
getElements(a,5);
sumOfElements(a,5);
cout<<"The sum is "<<sumOfElements(a,5)<<endl;
return 0;
}
//Getting array's elements
void getElements(int numbers[],int size_)
{
for (int i=0; i<size_; i++)
{
cout<<"numbers["<<i<<"]: ";
cin>>number;
numbers[i]=number;
}
cout<<'\n';
}
//Calculation the sum of array's elements
int sumOfElements(int numbers[],int size_)
{
for(int i=0;i<size_;i++)
{
sum+=numbers[i];
}
cout<<sum<<endl;
return sum;
}
any idea? thank you in advance!
You defined int sum globally and were calling sumOfElementstwice, so sum contained twice what you expected.
Here is a modified version of your code that does what you want:
#include <iostream>
using namespace std;
void getElements(int[], int);
int sumOfElements(int[], int);
int main() {
int numbers[5];
getElements(numbers, 5);
cout << sumOfElements(numbers, 5);
return 0;
}
void getElements(int numbers[], int size) {
for (int i = 0; i < size; i++) {
cin >> numbers[i];
}
}
int sumOfElements(int numbers[], int size) {
int sum = 0;
for (int i = 0; i < size; i++) {
sum += numbers[i];
}
return sum;
}
Here is a modified and simpler version of your program:
#include <array>
#include <iostream>
#include <numeric>
using namespace std;
int main(){
const int num_elements_to_sum = 5;
array<int, num_elements_to_sum> elements;
for(int i=0; i<num_elements_to_sum; ++i){
cin>>elements[i];
}
int sum = accumulate(elements.begin(), elements.end(), 0);
cout<<"Sum: "<<sum<<endl;
return 0;
}
C++ has a dedicated fixed size array container, use this instead of C-style arrays. This then allows to use standard library algorithms instead of your own implementation (e.g. accumulate).
Basically this is my code and it works fine. I just don't know how to print it in descending order. This code basically shows the odd numbers:1,3,5,7. I want it to be printed 7,5,3,1. I know I need use the sort function but I dont know how.
#include <iostream>
using namespace std;
void fillArray(int arr[], int &n);
void printArray(int arr[], int n);
void findSum(int arr[], int &n);
int main()
{
int n;
cin>>n;
int arr[n];
fillArray(arr,n);
printArray(arr,n);
findSum(arr,n);
return 0;
}
void fillArray(int arr[], int &n)
{
int j=1;
for(int i=0;i<n;i++)
{
if(j%2==1)
arr[i]=j;
else
i--;
j++;
}
}
void printArray(int arr[], int n)
{
for(int i=0;i<n;i++)
{
cout<<arr[i]<<", ";
}
}
void findSum(int arr[], int &n)
{
int sum=0;
for(int i=0;i<n;i++)
{
sum=sum+arr[i];
}
}
for(int i = n-1; i >= 0; i--)
{
cout << arr[i] << ", ";
}
example:
void printArray(int *tab, int size)
{
for (int i = size - 1; i >= 0; i--)
std::cout << tab[i] << std::endl;
}
int main() {
int tab[3] = { 1,2,3 };
printArray(tab, 3);
}
You should begin from last element array, and decrement iterator (i) to i == 0
You can simply use a sort function. There is one included with the algorithm header.
#include <algorithm> // this goes at the top
void printArray(int arr[], int n)
{
sort(arr, arr+n, [](int x, int y){return y<x;});
for(int i=0;i<n;i++)
cout << arr[i] << endl;
}
the [](int x, int y){return y<x;} part is just to make it descending. Normally it is y>x, at which point you can just omit the third parameter
Here is a repl:
https://repl.it/JQor/0
I know the logic how to merge two arrays but the problem is how to code.
This was my code n it is giving correct ans but my sir told me that do it again,please tell me what I have to add in this code,
#include<iostream>
using namespace std;
int mergeArrays(int array1[],int size1,int array2[],int size2);
int main()
{
const int size1=8;
const int size=12;
int arrayA[size1]={10,25,37,49,50,51,55,60};
int arrayB[size]={2,5,26,27,29,32,40,45,70,80,90,95};
mergeArrays(arrayA,size1,arrayB,size);
}
int mergeArrays(int array1[],int size1,int array2[],int size2)
{
int size3=size1+size2;
int *array3=new int[size3];
int k=0;
for(int i=0;i<size1;i++)
{
array3[k]=array1[i];
cout<<" "<<array3[k];
}
int j=0;
for(int i=size1;i<size2;i++)
{
array3[k]=array2[j];
}
for(int i=size1;i<size2;i++)
{
for(int j=0;j<size2;j++)
{
array3[i]=array2[j];
cout<<" "<<array3[i];
}
cout<<endl;
delete[]array3;
return array3[k++];
}
}
I had searched this in many places but could not corrected my code
I had written this code but it is not giving correct ans.
#include<iostream>
using namespace std;
int merge(int *a,int *b,int aSize,int bSize);
int main()
{
const int aSize={8};
const int bSize={12};
int arrayA[aSize]={10,25,37,49,50,51,55,60};
int arrayB[bSize]={2,5,26,27,29,32,40,45,70,80,90,95};
merge(arrayA,arrayB,aSize,bSize);
return 0;
}
int merge(int *a,int *b,int aSize ,int bSize)
{
int cSize=aSize+bSize;
int *c=new int[cSize];
int j=0,k=0;
int i=0;
while(i<=aSize&&j<=bSize )
{
if(a[aSize ]<=b[bSize])
{
c[k]=a[aSize];
k++;
i++;
}
else
{
c[k]=b[bSize];
k++;
j++;
}
}
for(int i=0;i<k;i++)
{
cout<<c[i]<<endl;
}
delete[]c;
return c[k++];
}
your sir request you do Merging two arrays in ascending order. so i think you should return a new array, fill with array1 and array2's element, and the elements should be ascending order. here is a implement.(suppose your input arraies is already in ascending order.)
#include <iostream>
using namespace std;
int mergeArrays(int array1[],int size1,int array2[],int size2, int outArray[]);
int main()
{
const int size1=8;
const int size=12;
int arrayA[size1]={10,25,37,49,50,51,55,60};
int arrayB[size]={2,5,26,27,29,32,40,45,70,80,90,95};
int outArray[size1+size];
int len = mergeArrays(arrayA,size1,arrayB,size, outArray);
cout <<" "<< len;
for (int i = 0; i< size1+size; ++i){
cout <<" " << outArray[i];
}
}
int mergeArrays(int array1[], int size1, int array2[], int size2, int outArray[])
{
int i=0, j=0, k=0;
int retSize = size1+size2;
while (k<retSize){
if (i==size1){// only left array2, copy it
for (; j<size2; ++j){
outArray[k++] = array2[j];
}
}else if (j == size2) { // only left array1, copy it
for (; i<size1; ++i){
outArray[k++] = array1[i];
}
}
else if (array1[i] > array2[j]){ // copy the min value to outArray
outArray[k++] = array2[j++];
}else{
outArray[k++] = array1[i++];
}
}
return k;
}
now, let's look at your first code:
int mergeArrays(int array1[],int size1,int array2[],int size2)
{
int size3=size1+size2;
int *array3=new int[size3];
int k=0;
for(int i=0;i<size1;i++)
{
array3[k]=array1[i]; // k is not changed, so you just assign array1's each value to array3[0]
cout<<" "<<array3[k];
}
int j=0;
// what's the purpose of this loop?
// and in loop, you don't use i, you just repeat set array3[0] = array2[0]!!
for(int i=size1;i<size2;i++)
{
array3[k]=array2[j];
}
for(int i=size1;i<size2;i++) // if array2's length bigger than array1's, will enter this loop.
{
for(int j=0;j<size2;j++)
{
array3[i]=array2[j]; // this just repeat assign array2's each value to array3[i]!!
cout<<" "<<array3[i];
}
cout<<endl;
delete[]array3;
return array3[k++]; // you delete array3, but at here you access it!! this will crash!
// also, in this for i loop, you have return, so it will only execute once.
}
// reach function end and no return if not enter for loop.
}
I haven't looked at your second code. I think you still need to do more study.
I'm trying to count the number of comparisons my quicksort algorithm makes for an array size of 500. I know that the best case for quicksort with partition is nlogn-n+1. So for an array size of 500, the best case number of component wise comparisons would be about 3983. However, when I run my code, I'm getting 2400 comparisons or so, depending on the array the random function generates. Am I counting the number of component wise comparisons wrong? Please help.
#include <iostream>
#include <string>
#include <stdlib.h>
using namespace std;
int count_500 = 0;
int partition(int *S,int l, int u);
void swap(int &val1, int &val2);
void Quicksort(int S[],int low, int hi);
void exchange(int list[], int p, int q);
int median_of_3(int list[], int p, int r);
void Quicksort_M3(int S[], int low, int hi);
int main()
{
int S1_500[500];
int S2_500[500];
int S3_500[500];
int S1_200[200];
int S2_200[200];
int S3_200[200];
int S1_8[8];
int S2_8[8];
int S3_8[8];
srand ( time(NULL) );
for(int i=0; i<500; i++)
{
S1_500[i] = rand()%1000;
S2_500[i] = rand()%1000;
S3_500[i] = rand()%1000;
}
for(int i=0; i<200; i++)
{
S1_200[i] = rand()%500;
S2_200[i] = rand()%500;
S3_200[i] = rand()%500;
}
for(int i=0; i<8; i++)
{
S1_8[i] = rand()%100;
S2_8[i] = rand()%100;
S3_8[i] = rand()%100;
}
Quicksort(S1_500,0,499);
for(int i=0; i<500; i++)
{
cout << S1_500[i] << endl;
}
cout << "Number of component wise comparisons is: " << count_500 << endl;
}
int partition(int *S,int l, int u)
{
int x = S[l];
int j = l;
for(int i=l+1; i<=u; i++)
{
if(S[i] < x)
{
count_500++; // Count the component wise comparison
j++;
swap(S[i],S[j]);
}
}
int p = j;
swap(S[l],S[p]);
return p;
}
void swap(int &val1, int &val2)
{
int temp = val1;
val1 = val2;
val2 = temp;
}
void Quicksort(int S[],int low, int hi)
{
if (low < hi)
{
int p = partition(S,low,hi);
Quicksort(S,low,p-1);
Quicksort(S,p+1,hi);
}
}
You want the count_500++; outside the if statement. You're only counting the comparisons, where the result is true.
Change
if(S[i] < x)
{
count_500++; // Count the component wise comparison
...
}
to
count_500++; // Count the component wise comparison
if(S[i] < x)
{
...
}