Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I am working on a c++ homework assignment for arrays and functions, and this is what I have so far and am not even sure if I am on the right path or not. These are the exact instructions..
Write a program to ask the user to enter a total of N numbers which you will store in main local array of Define N as a constant int and initialize it to 6. You will write the following functions:
FillArray( ) – accepts two inputs: (1) the array. (2) the array size. Returns nothing.
• Prompts the user to enter N elements (N = the array size and the variables you pass should have been defined as a constant int in main( )
• Use a for loop to enter and store the value of each element in the array
#include <iostream>
using namespace std;
int main(){
cout << "Enter 6 numbers for the array:" << endl;
FillArray();
return 0;
}
void FillArray(){
int n;
int array[6] = { 0, 0, 0, 0, 0, 0,};
void fillarray(const int n[], int size);
for (; n > 6; n++)
cin >> array[n];
cout << "Thank you\n";
}
Any suggestions or help would be appreciated.. thank you!
Write a program to ask the user to enter a total of N numbers which
you will store in main local array of Define N as a constant int and
initialize it to 6
int main()
{
const int N = 6;
int a[N];
//...
You will write the following functions: FillArray( ) – accepts two
inputs: (1) the array. (2) the array size.
//...
FillArray( a, N );
//...
void FillArray( int a[], int n )
{
cout << "Enter " << n << " numbers for the array: ";
for ( int i = 0; i < n; i++ )
{
cin >> a[i];
}
}
And the name of the function used in the program shall be declared before its using
void FillArray( int a[], int n );
int main()
{
//...
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I am writing C++ code to get the maximum number from an array of integers using a function.
My code is as follows, but it's not working and I can't seem to fix it, I would like to know, if possible, what's wrong with it:
#include<iostream>
using namespace std;
int maxfunc(int myArr, int size);
int main()
{
int size;
cout<<"Enterv the size of array: "<<size;
int *arr=new int [size];
for(int i=0; i<size; i++)
{
cout<<"Value No. "<<i+1<<" : ";
cin>>arr[i];
}
int max = maxfunc(arr,size);
cout<<"max = "<<max;
}
int maxfunc(int myArr, int size)
{
int largest=myArr[0];
for(int i=1; i<size; i++)
{
if(largest<myArr[i])
{
largest=myArr[i];
}
}
return largest;
}
In
int maxfunc(int myArr, int size)
The argument int myArr is an integer but you are using it as an array of integers.
I would use a pointer to the original array:
int maxfunc(int *myArr, int size)
Another problem that stands out is that you are using size variable uninitalized, my gess is that you are missing a cin >> size;:
cout<<"Enterv the size of array: ";
cin >> size;
Finally, and this is opinion based, this is the kind of program you would use in C, for C++ there are better tools for storing a manipulating data. For this case I would recommend std::vector or if the array is ment to have fixed size, std::array.
You did not entered the size of the array
int size;
cout<<"Enterv the size of array: "<<size;
int *arr=new int [size];
So the program has undefined behavior.
Also the function implementation is wrong because in general the user can pass the second argument equal to or less than 0.
The function should be declared like
size_t maxfunc( const int myArr[], size_t size);
^^^^^^^^^^^^^^^^^
and it should return the index of the maximum element.
The function can be implemented like
size_t maxfunc( const int myArr[], size_t size )
{
size_t largest = 0;
for ( size_t i = 1; i < size; i++ )
{
if ( myArr[largest] < myArr[i] )
{
largest = i;
}
}
return largest;
}
And in main you can write
size_t max = maxfunc( arr, size );
cout << "max = " << arr[max] << '\n';
delete []arr;
Do not forget to free the allocated memory.
Pay attention to that in C++ there is the standard algorithm std::max_element declared in the header <algorithm> that does the task. It returns iterator that points to the maximum element.
I am learning cpp on my own through a book named Programming with Cpp by John R. Hubbard, Phd. The example below is from the same source.
#include <iostream>
using namespace std;
void read(int [], int&);
void print( int [], int);
long sum (int [], int);
const int MAXSIZE=100;
int main(){
int a[MAXSIZE]={0}, size;
read (a,size);
cout << "The array has " <<size <<" elements: ";
print (a,size);
}
void read(int a[], int& n){
cout <<"Enter integers. Terminate with 0: \n";
n=0;
do{
cout << "a ["<<n<<"]: ";
cin >> a[n];
}
while (a[n++] !=0 && n<MAXSIZE);
--n; //don't count the 0
}
void print (int a[], int n){
for (int i=0; i<n; i++)
cout <<a[i]<<" ";
cout<<endl;
}
Based on the above code, I need to know:
1) Why is the array[MAXSIZE] made equal to 0 in the main() function? Is it ok to use it without initializing it?
2) What is the role of n=0 in the read() function?
1) Why is the array[MAXSIZE] made equal to 0 in the main() function? Is it ok to use it without initializing it?
Only the first element is set to 0.The others are default initialized to 0. In this case is would be ok to use it without initializing it, that is, even without the ={0}, but only if valid integers are provided.
2) What is the role of n=0 in the read() function?
The variable n is used to index through the parameter array a. Since C++ uses zero based indexing the first element of an array is at position 0, and that's why n is set to 0 initially.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
#include<iostream>
using namespace std;
//prototype
void fillArray(int[], int, int);
void printArray(int[], int);
int main()
{
srand(time(0)); // initialize random number generator
const int size = 5, SIZE=10;
int ar1[size], ar2[size],inc;
cout << "Enter the first value and increment for ar1: ";
cin >> ar1[0] >> inc;
cout << "Enter the first value and increment for ar2: ";
cin >> ar2[0] >> inc;
cout << "(1) ar1:\n";
fillArray(ar1, size, inc);
printArray(ar1, size);
system("pause");
return 0;
}
void fillArray(int ar[], int size, int inc)
{
for (int i = 1; i < size; i++)
{
ar[i] = ar[0] + inc;
cout << ar[size];
}
}
void printArray(int ar[], int size)
{
for (int i = 0; i < size; i++) {
cout << ar[i] << ' ';
}
cout << endl;
}
Ask the user to input ar1[0] and inc.the first one ar1[0] is settled by user input, and the rest is increased by inc.So if I enter 4 3 ,it should return 4 7 10 13 16. My outcome is like this:
I know there is something wrong with the function fillArray but I don't know how to fix it. Can anyone tell me the solution? Thanks
Yes there's something wrong with your function fillArray. It assumes that the first element is initialized (which is, indeed) but then doesn't initialize the other elements in ascending order, because you always add inc to the value of the first element. Moreover it prints out ar[size] which is out of bounds at every step (the first strange numbers in your output). You can fix with this:
void fillArray(int ar[], int size, int inc)
{
for (int i = 1; i < size; i++)
{
ar[i] = ar[i-1] + inc;
}
}
Or you can decide to pass the first value as a parameter too.
I assume you wanted to use size, even if you also declare another similar variable named SIZE with a different value. You should also consider using std::vectors instead of arrays as a safer a more practical option.
Finally, in your code you ask the user to input inc twice (overwriting it) before using it.
I need some help creating an array with 10 number that the user can pick. Had a post about this yesterday but misstook arrays for vectors..
Need to calculate the average value of the numbers, need pseudocode for it as well.
Any help would be awesome, I do have a school book but the array examples in it will just not work (as you can se in the code I'll add).
This is what I got sofar:
#include <iostream>
#include <array>
using namespace std;
int main()
{
int n[10];
for (int i = 0; i < 10; i++)
{
cout << "Please enter number " << i + 1 << ": ";
cin >> n[i];
}
float average(int v[], int n)
{
float sum = 0;
for (int i = 0; i < n; i++)
{
sum += v[i]; //sum all the numbers in the vector v
}
return sum / n;
}
system("pause");
}
the part to calculate the average I got help with from the last post I had. But everything else won't work "/ So basicly I need help to make a array with 10 user input numbers. Cheers
The only thing that you wrote correctly is function average. I would add qualifier const to the parameter of the function
#include <iostream>
#include <cstdlib>
using namespace std;
float average( const int v[], int n )
{
float sum = 0.0f;
for ( int i = 0; i < n; i++ )
{
sum += v[i]; //sum all the numbers in the vector v
}
return sum / n;
}
Or statmenet
return sum / n;
could be substituted for
return ( n == 0 ? 0.0f : sum / n );
Take into account that functions shall be defined outside any other functions and a function declaration shall appear before usage of the function.
You need not header <array> because it is not used. But you need to include header <cstdlib> because you use function system.
As it is written in your assigment you need enter arbitrary values for the array
int main()
{
const int N = 10;
int a[N];
cout << "Enter " << N << " integer values: ";
for ( int i = 0; i < N; i++ ) cin >> a[i];
cout << "Average of the numbers is equal to " << average( a, N ) << endl;
system( "pause" );
return 0;
}
int n[10]; - n is an array of ints, not strings, so why are you doing n[0] = "Number 1: ";? You should instead loop and ask for an input from the user.
After you do this, you should place average function outsude the main function and call it from the main.
I advise you to go through a basic tutorial.
Function definition should always be outside main.
int n[10] mean n is array of integers of size 10. They are not array of pointers of type char * to hold strings
There isn't a caller for function average. Subroutines work like, callers will call callee passing arguments to perform operations on them and return them back - pass by reference.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I need to write a program that is passed an array of int and its size, and with that it prints out the numbers that are above average. Can anyone help clear this question up for me? I am sitting here wondering what the heck it is asking, and I am still quite new to programming so i don't know what to do. Sorry I sound so unable, but I am just confused. Thank you to anybody who can help. This is all I have so far:
Here is the updated code, but I still can't figure out why there aren't multiple average values displaying out, or how to get the output value to be correct.
EDIT: Changed a couple int values in the average() function to floats, but there is still a problem with the total value's at the end
#include <iostream>
using namespace std;
int average(int values[],int size);
int main(){
int size;
int values[] = {1,2,3,4,5,6};
cout << "Please input the size of the array" << endl;
cin >> size;
int output = average(values, size);
if(values[size]>output){
cout << "The values above average are: " << output << endl;
}
return 0;
}
int average(int values[],int size){
float temp=0.0;
for(int i=0;i<size;i++){
temp += values[i];
}
float end=temp/size;
return end;
}
your supposed to create an array of intigers and pass it to the average function, and need to pass the size(so that you know how many times to loop). In the loop in your Average function add all the values to a temporary value, then divide by the count input-ed to the function.
//returns the average
int average(int Values[],int Size){
//perhaps declare a temporary value here
for(int i=0;i<Size;i++){
//add all the values up and store in a temporary value
}
//here divide by Size and return that as the average
}
this
if(values[size]>output){
cout << "The values above average are: " << output << endl;
}
should be replaced with something like:
for(int i=0;i<size;i++){
if(values[i]>output){
cout << "The values above average are: " << values[i] << endl;
}
}
Here is a less convoluted solution than those posted. Main feature: It actually does do what it should:
#include <iostream>
template<size_t N>
float average( const int (&value)[N] ) {
float total( 0.0f );
// Sum all values
for ( size_t index = 0; index < N; ++index )
total += value[index];
// And divide by the number of items
return ( total / N );
}
int main() {
int value[] = { 1, 2, 3, 4, 5, 6 };
// Calculate average value
float avg = average( value );
const size_t count = ( sizeof( value ) / sizeof( value[0] ) );
// Iterate over all values...
for ( size_t index = 0; index < count; ++index )
// ... and print those above average
if ( value[index] > avg )
std::cout << value[index] << std::endl;
return 0;
}
Live example at Ideone. Output:
4
5
6