Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor: Use vector for dynamic array size, add input validation, and modularize average calculationRefactor #176

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"name":"Local: AverageArray","url":"c:\\Users\\dinesh mishra\\OneDrive\\New_Folder\\HacktoberFest-2022\\arrays\\AverageArray.cpp","tests":[{"id":1723896197036,"input":"","output":""}],"interactive":false,"memoryLimit":1024,"timeLimit":3000,"srcPath":"c:\\Users\\dinesh mishra\\OneDrive\\New_Folder\\HacktoberFest-2022\\arrays\\AverageArray.cpp","group":"local","local":true}
47 changes: 33 additions & 14 deletions arrays/AverageArray.cpp
Original file line number Diff line number Diff line change
@@ -1,19 +1,38 @@
//Calculate Average of Array
#include<bits/stdc++.h>
#include <iostream>
#include <vector>
#include <numeric> // for accumulate
#include <limits> // for numeric_limits

using namespace std;
int main(){

int a[100],sum=0,n;
cout<<"How many element: ";
cin>>n;
cout<<"Enter numbers: ";
for(int i=0;i<n;i++){
cin>>a[i];
}
for(int i=0;i<n;i++){
sum+=a[i];
}
cout<<"Average is : "<<(float)sum/n<<endl;
float calculateAverage(const vector<int>& numbers) {
int sum = accumulate(numbers.begin(), numbers.end(), 0);
return static_cast<float>(sum) / numbers.size();
}

int main() {
int n;
cout << "How many elements: ";
cin >> n;

// Error handling for invalid input
if (n <= 0) {
cout << "Number of elements must be positive!" << endl;
return 1;
}

vector<int> a(n);
cout << "Enter numbers: ";
for (int i = 0; i < n; i++) {
while (!(cin >> a[i])) {
cin.clear(); // clear the error flag
cin.ignore(numeric_limits<streamsize>::max(), '\n'); // ignore the invalid input
cout << "Invalid input. Please enter an integer: ";
}
}

float average = calculateAverage(a);
cout << "Average is: " << average << endl;

return 0;
}
2 changes: 1 addition & 1 deletion arrays/alternateArrays.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ int main()
alternateArray(arr, size);
for (int i = 0; i < size; i++)
{
cout << arr[i];
cout << arr[i] << " ";
}
return 0;
}
2 changes: 1 addition & 1 deletion arrays/arraysum.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ using namespace std;

int main() {
int a[] = {2, 4, 6, 8}; //integer array
int len = sizeof(x) / sizeof(a[0]); //get array length
int len = sizeof(a) / sizeof(a[0]); //get array length
int sum = 0; //initialize sum with 0

for (int i = 0; i < len; i++) {
Expand Down
1 change: 1 addition & 0 deletions contributorList.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ Sahil Kumar @HAXAZE
SHRUTI RANI @ShrutiRani7029
Sahan Chandrabahu @SahanChandrabahu
Manasij Haldar @TheROCKoManz
VARSHA @Varshamishra56