quarter of a one-dimensional array

Pages: 12
Hi guys, how can i get the 3/4 of
one-dimensional array(vector) in a program?
corepower wrote:
Hi guys, how can i get the 3/4 of
one-dimensional array(vector) in a program?


Do you mean you want a sub-vector three-quarters of the size ...
or the 75th percentile ...
or something else?
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>

using namespace std;

int main()
{
  int vec[99], i, n;
  cout << "\nNr. elementelor = ";
  cin >> n;

  for (i = 0; i < n; i++) {
  cout << "\nElement = ";
  cin >> vec[i];
  
     
  }






}

I mean in a program of this type. The 3/4 or 0,75.
Last edited on
Actually i want to find the arithmetic mean of the odd numbers from the first 3/4 of the vector
corepower wrote:
Actually i want to find the arithmetic mean of the odd numbers from the first 3/4 of the vector


Make your mind up!
Last edited on
That's the full condition
I just need to know how to get the first 3/4 of the vector
If the (array) has 99 elements, how many elements would you like the "0.75" of it to have?
how many i introduce from the cin>>n
corepower wrote:
how many i introduce from the cin>>n


Let me ask you again ... if the (array) has 99 elements, how many elements would you like the "0.75" of it to have?

Maybe you would like to state the original question, NOT your interpretation of it.
Last edited on
the array doesnt have 99 elements, it have as much as i want to introduce from the keyboard, as you see in the program of above.
It's given a one-dimensional array (vector), find the arithmetic mean of the odd numbers from the first 3/4 of the vector.
This is the original condition
how many i introduce from the cin>>n

If you use n first elements from array that has 99 elements, then you use "n/99 of the array".

The n/99 is equal with 3/4, if and only if the user gives correct value for n.

If you want 3/4, then you calculate and use the correct n and do not ask it from the user.


[EDIT]
Ok. You have array with n elements. Surely you can calculate int k, where k/n is about 3/4?
Last edited on
Something like this?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <vector>
#include <iostream>
#include <numeric>

int main()
{
	size_t noElems {};

	std::cout << "How many elements: ";
	std::cin >> noElems;

	std::vector<int> vi(noElems);

	std::iota(vi.begin(), vi.end(), 1);		// Temp vector containing numbers 1 .. noElems

	const auto newsz {vi.begin() + vi.size() * 3 / 4};	// Just use 3/4 of vector
	size_t noOdd {};
	auto sum {std::accumulate(vi.begin(), newsz, 0, [&noOdd](auto init, auto a) {return noOdd += a % 2, init + a * (a % 2); })};

	std::cout << "Average is: " << (sum + 0.0) / noOdd << '\n';
}

nope, i want to do it with a loop and just with iostream
Corepower, if your array has 5 elements (i.e. you input 5 from the keyboard), what is three-quarters of that?

Note that 0.75 *5 is 3.75, nearest to 4 as a whole number,
whereas 5 * 3 /4 (with integer division) is 3

So, clarify your problem.
Ok. Just using a loop.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <vector>
#include <iostream>
#include <numeric>
int main()
{
	size_t noElems {};

	std::cout << "How many elements: ";
	std::cin >> noElems;

	std::vector<int> vi(noElems);

	std::iota(vi.begin(), vi.end(), 1);		// Temp vector containing numbers 1 .. noElems

	const auto newsz {vi.begin() + vi.size() * 3 / 4};	// Just use 3/4 of vector

	size_t noOdd {};
	int sum {};

	for (auto it = vi.begin(); it != newsz; ++it)
		if (*it % 2) {
			++noOdd;
			sum += *it;
		}

	std::cout << "Average is: " << (sum + 0.0) / noOdd << '\n';
}


Note that the code before L15 is just initializing a vector with some values for testing.
I think @corepower is trying to say that it's not that array(vector) but the other one.
omg guys why its so hard to understand
1.i introduce the number of elemets from the keyboard
2. i introduce the elements
3. the program returns me the arithmetic mean of the odd numbers from the first 3/4 of the numbers i introduced
Well, as long as n is divisible by 4 then either of seeplus's codes solves your problem.

Why is it so hard to understand ... well look back at your first post!
Pages: 12