[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 232
  • Last Modified:

Can't debug!!

This is a program my friend passed to me to check. I've found that there are a few errors such as array/pointer types but can't correct them. Can anyone help out? Here's the code, pls debug and tell me the errors. Thanx.

#include<iostream.h>

int search_ordered_array(const int [],int,int);
void sort_array(int [],int);
void input_array(int,int);


int main()
{
      const int arraysize=50;
      int a[arraysize]={0};
      int searchkey,element;


      sort_array(a,arraysize);
      
      cout<<"Enter integer search key:\n";
      cin>>searchkey;
      element=search_ordered_array(a,searchkey,arraysize);

      if(element!=-1)
            cout<<"Found value in element -->"<<element<<endl;
      else
            cout<<"Value not found"<<endl;

      return 0;
}


void input_array(int list, int score)//SOMETHING WRONG!!!
{
      list=50;
      int n;

      cout<<"\nPlease enter array size:\n";
      cin>>n;
      cout<<"Please enter values to the test score sheets:\n";
      cout<<"And enter (-1) to finish entering values:\n";
      for(int i=0;i<n;i++)
      {
            cin>>score[i];
      if (score[i]==-1)
            break;
      }

}

void sort_array(int a[],int arraysize)
{
      int hold,i;
      for(int pass=0;pass<arraysize-1;pass++)
            for(i=0;i<arraysize-1;i++)
                  if(a[i]>a[i+1])
                  {
                        hold   = a[i];
                        a[i]   = a[i+1];
                        a[i+1] = hold;
                  }

      cout<<"\nData in ascending order:\n";
      for(i=0;i<arraysize;i++)
            cout<<a[i]<<endl;
}
      

int search_ordered_array(const int array[],int key,int sizeofarray)
{
      for(int n=0;n<sizeofarray;n++)
            if(array[n]==key)
                  return n;
            
            return -1;
}
0
Neo78
Asked:
Neo78
  • 5
  • 4
  • 2
1 Solution
 
cgallagherCommented:
Your problem is that you tried to use score as an array without actually defining it as an array,
This code should fix the problem


#include<iostream.h>

int search_ordered_array(const int [],int,int);
void sort_array(int [],int);
void input_array(int,int);


int main()
{
      const int arraysize=50;
      int a[arraysize]={0};
      int searchkey,element;


      sort_array(a,arraysize);

      cout<<"Enter integer search key:\n";
      cin>>searchkey;
      element=search_ordered_array(a,searchkey,arraysize);

      if(element!=-1)
            cout<<"Found value in element -->"<<element<<endl;
      else
            cout<<"Value not found"<<endl;

      return 0;
}


void input_array(int list, int score[]) //score[] is an array...
{
      list=50;
      int n;

      cout<<"\nPlease enter array size:\n";
      cin>>n;
      cout<<"Please enter values to the test score sheets:\n";
      cout<<"And enter (-1) to finish entering values:\n";
      for(int i=0;i<n;i++)
      {
            cin>>score[i];
            if (score[i]==-1)
                  break;
      }

}

void sort_array(int a[],int arraysize)
{
      int hold,i;
      for(int pass=0;pass<arraysize-1;pass++)
      for(i=0;i<arraysize-1;i++)
            if(a[i]>a[i+1])
            {
                  hold   = a[i];
                  a[i]   = a[i+1];
                  a[i+1] = hold;
            }

      cout<<"\nData in ascending order:\n";
      for(i=0;i<arraysize;i++)
      cout<<a[i]<<endl;
}


int search_ordered_array(const int array[],int key,int sizeofarray)
{
      for(int n=0;n<sizeofarray;n++)
      if(array[n]==key)
            return n;

      return -1;
}
0
 
NorbertCommented:
If it is homework please be honest!
void input_array(int list, int score)//SOMETHING WRONG!!!
yes it is
try
void input_array(int list, int* score)

BTW:
input_array is never called
0
 
cgallagherCommented:
That was a bit stupid writing out all the code again when it was easier to write out the single line...
But on the homework line, Telling him the difference between the parameter being an array and a normal integer is not breaking the rules I would say?
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
NorbertCommented:
But someone who is able to check a program should know the difference between an array and a normal integer:)

Anyway - I know it sometimes happens that I can't see the forest because of the trees :)
0
 
NorbertCommented:
Helping him at this point will not break the rules - I think.
He did already know where the error was and as nietod says we may help him like a teacher would do.
I am sure a teacher would tell him that he should check the data types.
0
 
cgallagherCommented:
A teacher would point out an error like that, but I will try to be a little more careful in the future and maybe try and tease the solution out of the person rather than actually giving them the answer. My apologise for stepping in a little quick.
Regards,
C.
0
 
NorbertCommented:
>>cgallagher did you see that your answer and my first comment occures nearly the same time ?
If I decided to say something there was no other comment or answer
I did never want to flame you !!
we both want to help and we both said nearly the same.
0
 
Neo78Author Commented:
Thanx to both of you but who should I give the points to? cgallagher I suppose right?
Anyway, this is not my homework and I certainly know the error is the type for score but like Norbert said, you can't see the forest because of the trees....so believe me when I say it's my friend's....
0
 
NorbertCommented:
I think cgallagher should get the points.
He was the first.
The work looks like a homework so if it is not your own is it the homework of your friend ?
In that case we have the same problem because at last it is homework.
0
 
cgallagherCommented:
Thats very decent of you Norbert.
Neo, I'm not bothered who you give the points to. I won't be dissapointed no matter who you give them to. Maybe I should get them because I answered first, but if you look at the answering times you'll see that he was probably typing his answer while I was posting mine, so its a close call.
Who's answer did you use?
Are there any rules here that you can check? If so that will be your answer if you are still unsure.

Regards,
C.
0
 
Neo78Author Commented:
ok guys..thanx to u two
It was my last effort to help my friend...haha
But still when i ran the answer, there was a couple errors tho no one realised them even my friend......Oh well, thanx again and cya....
0

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

  • 5
  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now