1

so I have this simple program here and my instructor is asking for the following edits, and I can't see it:

Sorry, you are missing the point. Since all of your functions are using indexing from 1 to n, where n can be 100, you never use the element with index zero of the array. That means that the maximum number of values you will use in the array is 99. If the user really wants to enter 100 values, your solution then won’t work.

Can you fix your solution again?

 #include <iostream>
using namespace std;
int readnums(int v[]);
void findmaxmin(int v[],int n,int &mi,int &ma);
int findmidsum(int v[],int n,int mi,int ma);
int main()
{
const int ARRAYVALUE=100;
int v[ARRAYVALUE];
int n=readnums(v),mi,ma;
findmaxmin(v,n,mi,ma);
cout<<"Max= "<<ma<<endl;
cout<<"Min= "<<mi<<endl;
cout<<"Middle sum= "<<findmidsum(v,n,mi,ma)<<endl;

}
int readnums(int v[])
{
cout<<"How many numbers to enter: ";
int n=0;

cin>>n;
for(int a=1;a<=n;a++)
{
    cout<<"Enter no. "<<a<<": ";
    cin>>v[a];
}
return n;
}
void findmaxmin(int v[],int n,int &mi,int &ma)
{
ma=v[1];
mi=v[1];
for(int a=1;a<=n;a++)
{
    if(mi>v[a])mi=v[a];
    if(ma<v[a])ma=v[a];
}
}
int findmidsum(int v[],int n,int mi,int ma)
{
int s=0;
for(int a=1;a<=n;a++)
    if(v[a]!=mi && v[a]!=ma)
    s+=v[a];
return s;
}
9
  • So what is the question? How to check that the supplied index is in the range of [0, 99]? Commented Mar 10, 2016 at 16:52
  • 1
    You can use an assert() to check the parameters passed to the function. Commented Mar 10, 2016 at 16:53
  • 1
    Basically he's saying I'm using indexes in my function that range from 1 to n, and n could possibly be 100. And that indexes need to be from 0 to 99 Commented Mar 10, 2016 at 16:54
  • 1
    Instead of iterating from a=1 to a<=n iterate from a=0 to a<n. Commented Mar 10, 2016 at 18:25
  • 1
    @MustafaKadadah Check out my answer below! I've tested it and it seems to work well. Commented Mar 11, 2016 at 14:21

2 Answers 2

3

As I mentioned in my comment, you have to iterate from a=0 to a<n rather than from a=1 to a<=n. But you also have to modify the following lines:

ma=v[1];
mi=v[1];

...to be:

ma=v[0];
mi=v[0];

...or else, as you mentioned, the max calculation breaks (because v[1] does not exist when there is only one index in the array).

Here's the complete updated code:

#include <iostream>
using namespace std;
int readnums(int v[]);
void findmaxmin(int v[],int n,int &mi,int &ma);
int findmidsum(int v[],int n,int mi,int ma);
int main()
{
  const int ARRAYVALUE=100;
  int v[ARRAYVALUE];
  int n=readnums(v),mi,ma;
  findmaxmin(v,n,mi,ma);
  cout<<"Max= "<<ma<<endl;
  cout<<"Min= "<<mi<<endl;
  cout<<"Middle sum= "<<findmidsum(v,n,mi,ma)<<endl;
}

int readnums(int v[])
{
  cout<<"How many numbers to enter: ";
  int n=0;

  cin>>n;
  for(int a=0;a<n;a++)
  {
    cout<<"Enter no. "<<a+1<<": ";
    cin>>v[a];
  }
  return n;
}

void findmaxmin(int v[],int n,int &mi,int &ma)
{
  ma=v[0];
  mi=v[0];
  for(int a=0;a<n;a++)
  {
    if(mi>v[a])mi=v[a];
    if(ma<v[a])ma=v[a];
  }
}

int findmidsum(int v[],int n,int mi,int ma)
{
  int s=0;
  for(int a=0;a<n;a++) {
    if(v[a]!=mi && v[a]!=ma)
    s+=v[a];
  }
  return s;
}

You can test the above code out here: http://cpp.sh/6ogo

Sign up to request clarification or add additional context in comments.

1 Comment

Fantastic! Appreciate it bud!
0

Your array contains 100 int, therefore if the entered n is bigger than 99 your program might crash.

int main()
{
const int ARRAYVALUE=101;
int v[ARRAYVALUE];
int n=readnums(v),mi,ma;
findmaxmin(v,n,mi,ma);
cout<<"Max= "<<ma<<endl;
cout<<"Min= "<<mi<<endl;
cout<<"Middle sum= "<<findmidsum(v,n,mi,ma)<<endl;

}
int readnums(int v[])
{
cout<<"How many numbers to enter: ";
int n=0;
Do{
cout << "Enter a number in the range 1 - 100 :" << endl
cin>>n;
}
while (n > 100 || n < 1);
for(int a=1;a<=n;a++)
{
    cout<<"Enter no. "<<a<<": ";
    cin>>v[a];
}
return n;
}

fixed solution so the user can enter 100 values, your array contains 101 ints.

4 Comments

I see! Would I need that check in any other function? (n > 99 || n < 0)
From what I have seen you never assign another value to n therefore as long as you use n to go through your array you should be fine with only this check
Sorry, you are missing the point. Since all of your functions are using indexing from 1 to n, where n can be 100, you never use the element with index zero of the array. That means that the maximum number of values you will use in the array is 99. If the user really wants to enter 100 values, your solution then won’t work. Can you fix your solution again? So I guess that's not what he wants:
this is still looping from a = 1 so the first element never gets loaded

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.