Tags down


In c++ how do I go about using pointers to get the average of an array?

Date : October 16 2020, 03:08 PM
I wish this help you First of all your function getAverage() has different prototype than that you defined. And secondly you try to pass std::unique_ptr object into a function that instead expects a int*. But std::unique_ptr is a different type than int* and not implicitly convertible. So to pass int *use std::unique_ptr::get function. like
code :
#include <iostream>
#include <iomanip>
#include <memory>
using namespace std;

double getAverage(int *, int);

int main()
    int size = 0;
    cout << "How many scores will you enter? ";
cin >> size;

unique_ptr<int[]> ptr(new int[size]);

cout << endl;
int count = 0;

//gets the test scores

for (count = 0; count < size; count++)
    cout << "Enter the score for test " << (count + 1) << ": ";
    cin >> ptr[count];
    cout << endl;
//display test scores
cout << "The scores you entered are:";
for (count = 0; count < size; count++)
    cout << " " << ptr[count];
cout << endl;

double avg;
avg = getAverage(ptr.get(), size);
cout << setprecision(2) << fixed << showpoint << endl;

cout << "The average is " << avg << endl;

return 0;

double getAverage(int *ptr, int size)
double average1;
double total = 0;
for (int count = 0; count < size; count++)
    total = total + *(ptr + count);
average1 = total / size;

return average1;

Share : facebook icon twitter icon

Efficient way of calculating average difference of array elements from array average value

By : lucacard87
Date : March 29 2020, 07:55 AM
it helps some times I don't think you can do better than O(n log n).
Suppose the array were sorted. Then we could divide it into the elements less than the average and the elements greater than the average. (If some elements are equal to the average, that doesn't matter.) Suppose the first k elements are less than the average. Then the average distance is

Create an array from MySQL query, average the values of that array, then round that average up

By : Abhishek Sharma
Date : March 29 2020, 07:55 AM
like below fixes the issue Ok. Here is the code I have so far... , Your problem is here:
code :
$userrating = mysql_query($userratingquery) or die('SQL Error :: '.mysql_error());
$userrating = array();

Prompting the user for 10 inputs, sotring it in an array, finding the average, and printing numbers less than average

By : user7802004
Date : March 29 2020, 07:55 AM
this one helps. You're almost there, but you're only prompting once and placing the input in to a variable. You need to prompt 10 times if you want 10 numbers (otherwise you need to parse the input and extract 10 numbers from it).
Assuming you wanted a few methods of populating elmt through ask (here i use input instead of ask and nums instead of elmt--but we're talking minor semantics):
code :
// initialize array with 10 elements
var nums = new Array(10);
// iterate over each element and prompt for value
for (var i = 0; i < nums.length; i++){
  var input = prompt('Enter number ' + (i + 1) + ' of ' + (nums.length + 1));
  // place value (as a number) into the array
  nums[i] = new Number(input);
/* sum/average code */
// initialize array with 10 elements
var nums = new Array(10);
// prompt for 10 numbers
var input = prompt('Enter ' + (nums.length + 1) + ' numbers separated by commas:');
var values = input.split(',');
// check we have the right amount
if (values.length == nums.length){
  // iterate over the values
  for (var i = 0; i < nums.length; i++){
    // place the input (as a number) into the array
    nums[i] = new Number(values[i]);
/* sum/average code */
  function average(){
    // initialize array with 10 elements
    var nums = new Array(10);
    // iterate over the elements
    for (var i = 0; i < nums.length; i++){
      // grab <input> based on id attribute
      var input = document.getElementById('num' + i);
      // insert the value (cast as a number) in to the array
      nums[i] = new Number(input.value); // case to number
    /* sum/average code */

<p>Enter 10 numbers:</p>
<input type="text" id="num0" />
<input type="text" id="num1" />
<input type="text" id="num2" />
<input type="text" id="num3" />
<input type="text" id="num4" />
<input type="text" id="num5" />
<input type="text" id="num6" />
<input type="text" id="num7" />
<input type="text" id="num8" />
<input type="text" id="num9" />

<button onclick="average()">Click to average</button>

How to correctly use pointers to compute average

By : user2209164
Date : March 29 2020, 07:55 AM
I wish did fix the issue. Testing that your pointer doesn't point into a \0 is fine when you work with strings. Here you have an array of ints and a length so you can't really use a destination point to stop incrementing the pointer. You need to use that len just like in your first snippet but instead of accessing the ith element of your array, you can still increment the pointer. Something like this :
code :
int compute_average(int *avgs, int len) {
  int sum = 0;
  for (int i = 0; i < len; i++)
    sum += *(avgs++);
  return (int)(sum / len);

Passing an array of pointers and return an array of values pointed by those pointers

By : Roberto Veloso
Date : March 29 2020, 07:55 AM
Does that help I want to write a function that passes an array of n pointers to float and returns a newly created array that contains those n float values. Here is the code: , Declaring b as
Related Posts Related Posts :
  • C++ - difference between first two elements
  • Repeated inheritance and static_cast
  • C++ template class, how to declare a copy constructor for a specific situation?
  • return 1 return 0 in case of factorial
  • Best way to hash a vertex
  • How append rectangle to QGraphicView
  • Extract points from PointCloud with PCL
  • Handling gcc's noexcept-type warning
  • Floating point math going wrong using cpp_bin_float from boost::multiprecision
  • Optimise a recursive function
  • Omit the newline character in reading file with c++
  • How should I manage ::std::cout after changing file descriptor 1 to refer to a different file?
  • Inheriting traits classes after a slight change in template type
  • How do I specify different libraries for a static Qt project?
  • Parent class turned into subclass Pointers C++
  • alternative of if else-if else statement c++
  • SetProgressValue() not working within ConEmu
  • Debugging compiler failure
  • Why My Applicaion cannot display unicode character correctly?
  • "Destructor already defined" with a specialized destructor
  • Is there a way to replace a function within a method in c++
  • MySQL c++ connector how to retrieve auto increment keys from insertion query
  • What is minimal work needed to reconnect existing socket to new server?
  • How to print to file using ostream_iterator?
  • Using cin.get() doesn't seem to read the character I'm expecting. What's wrong?
  • C++ template class with non-type parameter: how to overload the assign operator?
  • why compiler is not showing error when I have two functions one will take base class and one will take derived class as
  • map.find() not finding key c++
  • Determining if `x` hours have elapsed since start
  • chaining method of same object
  • C++ any iterator with a specific value type?
  • C++ hash_map.clear() is slow if compiled in debug configuration
  • How do you multiply two arrays with repeated addition in C++?
  • Calling Winspool API GetJOB issue
  • Get nested class members without accessors or mutators
  • Initialize vector member with the parameters of constructor
  • How to convert a String to a char * in Arduino?
  • Determine if a number contains a digit for class assignment
  • How to Pass a String in a function?
  • D3D12 unavoidable leak report
  • Iterate sorted list and count distinct numbers
  • i am not understanding the syntax of this dfs implementation
  • How to do a custom 2D Kernel on Filter2D using 1D gaussian kernel
  • Call/move a function from inside an "if loop" of another function - Qt, C++
  • Smart Pointers for Binary Tree
  • Serial port ReadFile receives repeated data
  • Preserve indentation in C++ comments in vim
  • qt chart move view with pressed middle mouse button
  • Why is the code "foo::foo::foo::foob" compiling?
  • Are type aliases used as type of function parameter part of the function signature?
  • Test if all elements are equal with C++17 fold-expression
  • error: no member named 'rank_' in 'EndIndex'
  • Uniform scaling on array of point around average point c++
  • shadow
    Privacy Policy - Terms - Contact Us © soohba.com