C++ Arrays and elements - c++

my program is focusing on Array elements for this lab, but I am not sure on how to set my average to a specific number that is requested. Any guidance would be helpful for this post
#include "pch.h"
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
int main()
{
int arr[10], n, i, max, min, avg;
cout << "Enter the size of the array: ";
cin >> n;
cout << "Enter the elements of the array: ";
for (i = 0; i < n; i++)
cin >> arr[i];
max = arr[0];
for (i = 0; i < n; i++)
{
if (max < arr[i])
max = arr[i];
}
min = arr[0];
for (i = 0; i < n; i++)
{
if (min > arr[i])
min = arr[i];
}
avg = arr[0];
for (i = 0; i < n; i++)
{
if (avg > arr[i])
avg = arr[i];
}
cout << "Largest element: " << max;
cout << "Smallest element: " << min;
cout << "Average element: " << avg;
}

I would recommend using the += operator in your for loop and then dividing by n.
float sum = 0;
float avg = 0;
for(i = 0; i < n; i++)
{
sum += arr[i];
}
avg = sum / n;
Also, I would recommend using either a float or double instead of using an int for your average because otherwise you're doing integer division which will cut off the decimal. i.e. 5 / 2 = 2

Related

How to get a range of elements in an array C++

I need to calculate the sum of negative elements of an array located between the largest and the smallest elements. Here's the code I've got so far:
#include <iostream>
using namespace std;
int main() {
int i = 0;
int arrSize = 0;
cout << "Enter an array size" << endl;
cin >> arrSize;
if (arrSize <= 0) {
cerr << "Entered number is negative or 0" << endl;
return 0;
} // check if arrSize is 0 or negative, print error if so
double* arr = new double [arrSize]; //declare array
for (i = 0; i < arrSize; i++) {
cout << "Enter array element " << i+1 << endl;
cin >> arr[i];
} // get elements of array
int max = arr[0];
for (int i = 1; i < arrSize; i++) {
if (max < arr[i])
max = arr[i];
} //find max value of array
int min = arr[0];
for (int i = 1; i < arrSize; i++) {
if (min > arr[i])
min = arr[i];
} //find min value of array
How would I go about finding the positions of max and min elements and getting the range in between them?

c++ Selection sort isn't working in specific cases

I've created a program that uses selection sort to sort integers in an array. In specific cases, such as if I input that I want an array of length 3 and then have "2 1 3" as my input, it outputs it in the same order. It's working for other inputs, but every now and then for some reason it wont sort a list of integers correctly.
My code is as follows:
'''
#include <iostream>
using namespace std;
void sort(int[], int);
void swap(int&, int&);
void swap(int &a, int &b){
int temp;
temp = a;
a = b;
b = temp;
}
void sort(int nums[], int n){
int j, min, i;
for (i = 0; i < n - 1; i++){
min = i;
for(j = i + 1; j < n; j++){
if(nums[j] < nums[min]){
min = j;
}
swap(nums[min], nums[i]);
}
}
}
int main(){
int n, i;
cout << "Enter how many numbers you want to sort" << endl;
cin >> n;
int nums[n];
cout << "Enter the integers seperated by a space: ";
for(i = 0; i < n; i++){
cin >> nums[i];
}
cout << "Array Before Sort: ";
for (i = 0; i < n; i++){
cout << nums[i] << ", ";
}
sort(nums, n);
cout << "\nArray after sort: ";
for(i = 0; i < n; i++){
cout << nums[i] << ", ";
}
return 0;
}
'''
Place your swap(nums[min], nums[i]); statement outside of inner loop, like this
void sort(int nums[], int n){
int j, min, i;
for (i = 0; i < n - 1; i++){
min = i;
for(j = i + 1; j < n; j++){
if(nums[j] < nums[min]){
min = j;
}
}
swap(nums[min], nums[i]);
}
}

Smallest composite number in array

I am totally new to programming and I am bit stuck on my code. I wrote code where I wanted to find smallest composite number in array(using only low-level arrays). When I wrote down like size of array 3 and enter 1 2 77, than it throws out random 16. Can you explain why is this happening and perhaps give some solution how to fix this.
#include<iostream>
using namespace std;
int fun(int n)
{
int arr[n];
int mini = arr[0];
for (int i = 0; i < n; i++)
cin >> arr[i];
for (int i = 0; i < n; i++)
{
for (int j = 2; j < arr[i]; j++)
{
if (arr[i] % j == 0)
{
if (mini > arr[i])
{
mini = arr[i];
}
else
{
mini = mini;
}
break;
}
}
}
return mini;
}
int main()
{
int n;
cout << "Size of array: ";
cin >> n;
cout << "Write " << n << " numbers: " << fun(n) << endl;
return 0;
}

Squaring numbers in array, finding the sum and the biggest number

I have a task to square all the elements of array, separate them with ",", then find a sum of the squared array and find the biggest number of it. I managed to square them and find the sum, but I can't find the biggest number and the program is also printing "," at the end of new array.
This is my code:
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
int a[10];
int n,sum=0,kiek=0,max=a[0];;
cin>>n;
for(int i=0;i<n;i++)
{cin>>a[i];
a[i]*=a[i];
sum=sum+a[i];
}
for (int i = 0 ; i < n ; i++)
{ cout <<a[i] << ","; }
cout<<endl ;
cout<<"suma " <<sum;
cout<<endl;
for(int i=0;i<10;i++)
{if(max<a[i])
{
max = a[i];
}
}
cout<<"max "<<max;
return 0;
}
This is the screenshot of my program result when I run it
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
int a[10];
int n, sum = 0; // Remove some unused variables
// Input //
cin >> n;
for(int i = 0; i < n; i++){
cin >> a[i];
a[i] *= a[i];
sum += a[i];
}
// List a[] and sum //
for (int i = 0 ; i < n - 1 ; i++) {
cout << a[i] << ", ";
}
cout << a[n - 1] << endl; // Just for a little beauty
cout << "suma " << sum << endl;
// Find Max //
int max = a[0]; // max should be declared there,
// because a[0] has not entered data at the first
for(int i = 0; i < n; i++) { // use n, not 10
if(a[i] > max){
max = a[i];
}
}
cout << "max " << max;
return 0;
}
Unchecked.
Please add indents, spaces and comment, this is a good habit.
Comment : If you are going to get the size of the array at run-time, it is better to use STL containers or pointers. Your issue lies here :
---> for(int i=0;i<10;i++)
{if(max<a[i])
Good luck.

How do I put any value X after all the minimums in an array?

If I enter an array , at first the code finds the minimums then I want to put zeroes after all the minimums . For example
given an array = 1,1,3,1,1
As we see 1s are the minimum so the result should be = 1,0,1,0,3,1,0,1,0
CODE
#include <pch.h>
#include <iostream>
int main()
{
int min = 10000;
int n;
std::cout << "Enter the number of elements (n): "; //no of elements in the
std::cin >> n; //array
int *array = new int[2 * n];
std::cout << "Enter the elements" << std::endl;
for (int i = 0; i < n; i++) {
std::cin >> array[i];
if (array[i] > min)
min = array[i];
}
for (int i = 0; i < n; i++) {
if (array[i] == min) { // Not very clear about this
for (int k = n; k > i; k--) // part of the code, my teacher
array[k] = array[k - 1]; //explained it to me , but i
array[i + 1] = 0; // didn't understand (from the
i++; // `for loop k` to be precise)
n++;
}
std::cout << array[i] << ", 0";
}
return 0;
}
But my answer doen't put zeroes exactly after minimums
There are few issues in your code, first of all your min is wrong. I have fixed your code with comments on fixes I have made. Please take a look :
#include "stdafx.h"
#include <iostream>
int main()
{
int min = 10000;
bool found = 0;
int n;
std::cout << "Enter the number of elements (n): "; //no of elements in the
std::cin >> n; //array
int *array = new int[2 * n];
std::cout << "Enter the elements" << std::endl;
for (int i = 0; i < n; i++) {
std::cin >> array[i];
if (array[i] < min) //< instead of >
min = array[i];
}
for (int i = 0; i < n; i++) {
if (array[i] == min)
{
for (int k = n; k > i; k--)
{
array[k] = array[k - 1];
}
array[i + 1] = 0;
i++; //increment i here because you don't want to consider 0 that you have just added above.
n++; //since total number of elements in the array has increased by one (because of 0 that we added), we need to increment n
}
}
//print the array separately
for (int i = 0; i < n; i++)
{
std::cout << array[i];
if (i != n - 1)
{
std::cout << ",";
}
}
return 0;
}
The first issue was in the calculation of min: < instead of >.
Another problem if that you are modifyng the paramers iand ninside the loop. This is rather dangerous and implies to be very cautious.
Another issue was that it should be i++; n++; instead of i--,n--;
Here is the code:
// #include <pch.h>
#include <iostream>
int main()
{
int min = 1000000;
int n;
std::cout << "Enter the number of elements (n): "; //no of elements in the
std::cin >> n; //array
int *array = new int[2 * n];
std::cout << "Enter the elements" << std::endl;
for (int i = 0; i < n; i++) {
std::cin >> array[i];
if (array[i] < min)
min = array[i];
}
for (int i = 0; i < n; i++) {
if (array[i] == min) { // Not very clear about this
for (int k = n; k > i; k--) // part of the code, my teacher
array[k] = array[k - 1]; //explained it to me , but i
array[i + 1] = 0; // didn't understand (from the)
i++;
n++;
}
}
for (int i = 0; i < n; i++) {
std::cout << array[i] << " ";
}
std::cout << "\n";
return 0;
}