C++ | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3

C++

We have an n-element integer array. Find the arithmetic mean of the even-indexed elements

18th Feb 2024, 10:08 AM
Balaxanim
Balaxanim - avatar
8 Answers
+ 2
For searching of even elemets you could take ie. modulo method: if(x%2 == 0) {your code}
18th Feb 2024, 10:53 AM
JaScript
JaScript - avatar
+ 2
Balaxanim your code have a lot of errors. syntax: int, for, s, return are supposed to be lowercase. your array a[n] is never initialized. your for loop is doing nothing to the sum or the array. you are not displaying your s, which remains 0 throughout. do you really need to define n? why not just use a[10]?
18th Feb 2024, 10:42 AM
Bob_Li
Bob_Li - avatar
+ 1
What is your question? First of all, do you know the artmetic definition of that what you ask? Can you tell this definition and data input output with a small arrays?
18th Feb 2024, 10:16 AM
JaScript
JaScript - avatar
+ 1
Thank you 🩵
18th Feb 2024, 10:59 AM
Balaxanim
Balaxanim - avatar
0
#include <iostream> #define n 10 Using namespace std; Int main () { Int a[n], s=0; For (int i=0;i<n; i+=2) S+= a[i]; Return 0; } This is Odd index element but I need an even index element but it doesn't work, what did I do wrong, what should I fix?
18th Feb 2024, 10:23 AM
Balaxanim
Balaxanim - avatar
0
I know syntaxs supposed to be lowercase. I have an exam tomorrow I need to raise I didn't fully understand the question, so I wrote it here.
18th Feb 2024, 10:55 AM
Balaxanim
Balaxanim - avatar
0
Hi
20th Feb 2024, 5:05 AM
Aditya Mohanty
Aditya Mohanty - avatar
0
To reduce the time complexity to check if the number is even or not....u can use & operator....if (n& (n-1))==0) Then n is even number
21st Feb 2024, 5:04 PM
Pranshu Sachan