prefix adding solution. Higher altitude solution
// calculate the total of the sum of prefix
#include <iostream>
using namespace std;
int main() {
int arr[5] = {1, 2, 3, 4, 5}; // Example array
int prefix_sum = 0; // Array to store prefix sums
for(int i = 0; i < sizeof(arr) / sizeof(arr[0]); i++) {
int sum = 0;
for(int j = 0; j <= i; j++) {
sum += arr[j];
}
prefix_sum = sum;
}
// Optional: Print the prefix sums to verify the result
cout << prefix_sum;
return 0;
}
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! OR !!!!!!!!!!!!!!!!!!!!
//
#include <iostream>
int main() {
int arr[5]= {1,2,3,4,5};
int storeSum = 0;
for(int i =0;i<sizeof(arr)/sizeof(arr[0]); i++){
storeSum += arr[i];
}
// Write C++ code here
std::cout << storeSum <<"" ;
return 0;
}
++++++++++++++++
//calculate the highest altuited of the bicker achived
#include <iostream>
#include <string>
using namespace std;
int main() {
string str = "hello"; // The input string
int store[26] = {0}; // Array to store frequency of each letter, initialized to zero
// Calculate frequency of each character using a traditional for loop
for (int i = 0; i < str.length(); i++) {
char c = str[i]; // Access the character at index i
if (c >= 'a' && c <= 'z') {
store[c - 'a']++; // Increment the count for this character
}
}
// Display the frequency of each character
cout << "Character Frequencies:\n";
for (int j = 0; j < 26; j++) {
if (store[j] > 0) { // Only display characters that appear in the string
cout << char(j + 'a') << ": " << store[j] << endl;
}
}
return 0;
}
----------------------------------------------------------OR------------------------------------------------------
#include <iostream>
#include <string>
using namespace std;
int main() {
string str = "hello"; // The input string
int store[26] = {0}; // Array to store frequency of each letter, initialized to zero
// Calculate frequency of each character using a traditional for loop
for (int i = 0; i < str.length(); i++) {
char c = str[i]; // Access the character at index i
if (c >= 'a' && c <= 'z') {
store[c - 'a']++; // Increment the count for this caharacter
}
}
// Display the frequency of each character
cout << "Character Frequencies:\n";
for (int j = 0; j < sizeof(store)/sizeof(store[0]); j++) {
if (store[j] > 0) { // Only display characters that appear in the string
cout << char(j + 'a') << ": " << store[j] << endl;
}
}
return 0;
}
Comments
Post a Comment