How to write the sum of counter[i] using for loops.

Im a newbie. And i want to know how can i add up the sum of array using a for loop and stop until a number.

E.g. i want to add up these counter until a number, e.g. 8. The total of these counter let say 15. How can i use a loop to add them up one by one and break it once >=8 is happened. please helpppppp.

counter[1]+counter[2]+counter[3]+counter[4]+counter[5]+counter[6]+counter[7]+counter[8]+counter[9]+counter[10]
+counter[11]+counter[12]+counter[13]+counter[14]+counter[15]+counter[16]+counter[17]+counter[18]+counter[19]+counter[20]
+counter[21]+counter[22]+counter[23]+counter[24]+counter[25]+counter[26].

Thankssss
Last edited on
closed account (SECMoG1T)
1
2
3
4
5
6
7
8
9
10
11
12
13
int arry[10]{1,2,3,4,5,6,8,7,12,43};
int sum=0, index =0;

while(index < 10)
{
     if(arry[index] >= 8)
        break;
    
     sum += arry[i];
     ++index;
}

std::cout<<""sum is: "<<sum; 
Last edited on
@Yolanda
Thank you so much for your reply. I will try that.
But i would like to know can i write sth like that?

for (int i=0; i<27; i++){
counter[i]+=counter[i];
if (counter[i]>=8)
break;}

But it only output 0.
Can i ask bout the problem of this ?
Last edited on
closed account (SECMoG1T)
i think you might be having a logic error.

1
2
3
4
5
6
7
8
9
for (int i=0; i<27; i++) //this loop is correct
{
    counter[i]+=counter[i];///this is the same as arry[i]+=arry[i]  or arry[i] = arry[i]*2; i dont know if this is what you want.
                                       ///did you intend to do smthng like sum += arry[i]; not sure/
    
   if (counter[i]>=8)/// if you don't want counter[i] be included in the summation move this line to after the opening brace '{'.
       break;
} 


can you post you current working code, i'll try to help
Last edited on
@Yolanda
OMGG THANK YOU SO MUCH FOR DOING THISSSS !!
Here's my program. I can't thank you enough!!!
i thought that counter[i]+=counter[i] is a way of adding counter[1]+....+counter[26], is it wrong?

#include <iostream>
using namespace std;

int main(){
char msg[50];
int counter[27];

for (int i=0; i<50; i++){
msg[i]='\0';
}
cin.getline(msg,50);

for (int j=0; j<28; j++){
counter[j]=0;
}



for (int k=0; k<50; k++){
if (msg[k]>='A' && msg[k]<='Z')
msg[k]+='a'-'A';
if (msg[k]>='a' && msg[k]<='z'){
counter[int(msg[k])-96]++;
}
}

Here i want to add that part. Cuz my Question actually requires me to find the middle alphabet among a message input. The input will be according to alphabetical order, e.g. abeefgiiiimnopq by the formula N/2 +1. So if i input let say 15 alphabet, the middle one is 8. i.e. letter i. My aim is to find the letter just at the point >or=8.



}

Last edited on
closed account (SECMoG1T)
//So if i input let say 15 alphabet, the middle one is 8.
this could be way easy...
but have tried reading your code \nd the is something m no getting,,, if you could post the question atleast i could be of more help than i can offer now from your comment.

@Yolanda
Topic archived. No new replies allowed.