#include <iostream>
#include<cmath>
usingnamespace std;
int main()
{
//DATA ABSTRACTION:
int n;
int sumNum=0;
int minNum =1000;
int maxNum =-1000;
//INPUT:
cout<<"Input the number of elements to store in the array: ";
cin>> n;
cout<<n;
int inputArr[n];
cout<<"\nInput "<<n<<" integers: \n"<<endl;
//PROCESS:
for(int i=0; i < n; i++)
{
cout<< "integer - " << i << " : ";
cin >> inputArr[i];
cout<< inputArr[i]<<"\n";
}
cout << "\nThe values stored into the array are :\n";
for(int i = 0; i < n; i++)
{
cout << inputArr[i] << " ";
}
cout<<endl;
cout << "\nThe values stored into the array in reverse are :\n";
for(int i = n-1; i >= 0; i--)
{
cout << inputArr[i] << " ";
}
cout<<endl;
for(int i=0; i<n; i++)
{
sumNum += inputArr[i];
if(inputArr[i] > maxNum) maxNum = inputArr[i];
if(inputArr[i] < minNum) minNum = inputArr[i];
}
int cntArr[25+1] = {0};
int value;
for(int i = 0; i<n; i++)
{
value = inputArr[i];
cntArr[value]++;
}
int dupNum=0;
for(int val=0; val<=25; val++)
{
if(cntArr[val] > 1)
{
dupNum = dupNum + (cntArr[val]-1);
}
}
//OUTPUT:
cout<<"\nThe sum of all elements of the array is "<<sumNum;
cout<<"\nThe total number of duplicate elements in the array is ";
cout<<dupNum;
cout<<"\nThe maximum and minimum element in the array are ";
cout<<maxNum<<" , "<<minNum;
//ASSUMPTIONS:
return 0;
}
this issue that I'm having is with the sumNum and dupNum parts. The code runs correctly, but when i get into sum higher numbers the the sumNum value is off.
Input the number of elements to store in the array: 17
Input 17 integers:
integer - 0 : -49
integer - 1 : 36
integer - 2 : 36
integer - 3 : 37
integer - 4 : 40
integer - 5 : -23
integer - 6 : 43
integer - 7 : 29
integer - 8 : -10
integer - 9 : 15
integer - 10 : -17
integer - 11 : 29
integer - 12 : 24
integer - 13 : -38
integer - 14 : -34
integer - 15 : 5
integer - 16 : 23
The values stored into the array are :
-49 36 36 37 40 -23 43 29 -10 15 -17 29 24 -38 -34 5 23
The values stored into the array in reverse are :
23 5 -34 -38 24 29 -17 15 -10 29 43 -23 40 37 36 36 -49
The sum of all elements of the array is 147
The total number of duplicate elements in the array is 0
The maximum and minimum element in the array are 43 , -49
Input the number of elements to store in the array: 17
Input 17 integers:
integer - 0 : -49
integer - 1 : 36
integer - 2 : 36
integer - 3 : 37
integer - 4 : 40
integer - 5 : -23
integer - 6 : 43
integer - 7 : 29
integer - 8 : -10
integer - 9 : 15
integer - 10 : -17
integer - 11 : 29
integer - 12 : 24
integer - 13 : -38
integer - 14 : -34
integer - 15 : 5
integer - 16 : 23
The values stored into the array are :
-49 36 36 37 40 -23 43 29 -10 15 -17 29 24 -38 -34 5 23
The values stored into the array in reverse are :
23 5 -34 -38 24 29 -17 15 -10 29 43 -23 40 37 36 36 -49
The sum of all elements of the array is 146
The total number of duplicate elements in the array is 2
The maximum and minimum element in the array are 43 , -49
dupNum is off in the above input, but have no idea why maybe something dealing with double digits, or negative values. Thanks in advance for any help.
Line 18 is a variable-sized array, not legal in C++. There are some compilers that allow it, though. The size of an array must be a compile-time constant.
If you want a run-time dynamic array either instantiate your array on the free store/heap with new[]: int* inputArr = new(int[n]);
or use a std::vector.
If you go with an array on the heap you need to make sure you clean up the memory with delete[] inputArr; when you are done with the array.
Your compiler is doing you no favors allowing non-standard behavior such as variable-sized arrays.
You create a variable-sized array on line 18, but then correctly create a compile-time sized array line 49. You use elements of your variable-sized array to manage elements of the const array.
Line 54 will have the array going out of bounds if inputArray has any elements that contain a value less than zero or greater than 25.
At best you will get undefined behavior going out of bounds. You'd be fortunate should you get a seg fault when trying to access an invalid element in cntArr.
Yes, a seg fault is better than UB. You'll know exactly where the problem is with a seg fault. With UB the program either appears to work, or you get a failure in a different location.
this looks like home-work, and early on many students are not allowed to use vectors and do not yet know of pointers.
in that case, one way to deal with the problem of line 18 is an oversized array.
for example if you are dealing with a few hundred items at most in the scope of the problem, then make the array have a size of 1000 or so. Then use your N variable to know how many of the elements are actually USED and the rest are wasted space, and that is OK for this approach.
it will still fail if you decide to run a million items, but as long as your default size is more than is likely to be needed, its good enough for schoolwork (and even some real world problems).
the right thing to do is to use a variable sized container, but you will get to that in time.
for that matter, C++ provides built in summation and more as well; your whole program can be reduced to a few lines of built in code. Other things you could do now; things like picking off the max and min can be done as the data is created or entered rather than after the fact, saving processing effort. You are doing it the 'hard way' to learn how to do things, and that is a good thing -- sorta like how in math they make you add up rectangle areas in calc 1 or that goofy division derivative formula that can be done 1000 times easier with a multiply, you do it all the hard way to see how it works.
#include <iostream>
#include<cmath>
usingnamespace std;
int main()
{
//DATA ABSTRACTION:
int inputArr[25];
int n;
int sumNum=0;
int dupNum=0;
int minNum =1000;
int maxNum =-1000;
//INPUT:
cout<<"Input the number of elements to store in the array: ";
cin>> n;
cout<<n;
cout<<"\nInput "<<n<<" integers: \n"<<endl;
every thing is still running correctly, but im still having an issue with finding the duplicate elements. However, i did find a fix
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
map<int,int>cntArr;
int value;
for(int i = 0; i<n; i++)
{
value = inputArr[i];
//increment of value of key (element)
cntArr[value]++;
}
//iterate in the map
for(auto i:cntArr)
{
//if frequency > 1
if(i.second > 1)
{
//increment dupNum by 1
dupNum = dupNum + 1;
}
}
this works, but we didn't go over this in class. If someone can explain to me this code and tell me if I'm doing it correctly I'd apricate alot thanks