PrintTheArray功能无法打印

时间:2017-11-12 23:53:04

标签: c++ arrays function

基本上我有这个打印菜单的程序,并调用各种函数来对数组做不同的事情。我需要使用getSize来声明数组的大小,但是我的老师在主函数中不需要任何其他内容。当我使用getSize时,它似乎有效,但是PrintTheArray无法打印任何内容......建议?同样抱歉格式化。

#include<iostream>
#include <cmath>
#include <cstdlib>

using namespace std;

int size; //global variable size
int a[100]; //global array

int getSize()
{
int size;
cout << "Array Size: ";
cin >> size;
return size;
cout << endl;
}

int sequentialSearch(int n)
{
for(int i=0;i<100;i++)
{
    if(n==a[i])
    {
        return (i+1);
}
}
return -1;
}

int binarySearch(int n)
{
int low=0,high=100,mid;
while(low<=high)
{
    mid=(low+high)/2;
    if(a[mid]==n)
    {

        return mid+1;
    }
    else if(a[mid]>n)
    high=mid-1;
else if(a[mid]<n)
    low=mid-1;
}
return -1;
}

 void sort1()
 {
int i, j;
for (i = 0; i < 100-1; i++)     
{
   for (j = 0; j < 100-i-1; j++)
       if (a[j] > a[j+1])
       {
           int temp=a[j];
           a[j]=a[j+1];
           a[j+1]=temp;
       }
}
}

void sort2()
{
int i, j, m;
for (i = 0; i < 100-1; i++)
{
    m = i;
    for (j = i+1; j < 100; j++)
    {
      if (a[j] > a[m])
        m = j;
    }
    int temp=a[m];
    a[m]=a[i];
    a[i]=temp;
    }
}


void printMenu()
{
cout<<"0. Exit\n";

cout<<"1.Get the size needed for todays use of the array \n";

cout<<"2.Fill an array with random numbers from 1-100 \n";

cout<<"3.Print the array with position numbers \n";

cout<<"4.Sort the array in ascending sequence \n";

cout<<"5.Sort the array in descending sequence \n";

cout<<"6.Sequential search of the array for a target\n";

cout<<"7.Binary search of the array for a target\n";
}

void printTheArray()
{
for(int i=0;i<size;i++)
{
    cout<<a[i]<<" is at position :"<<i+1<<endl;
}
}

void fillWithRandom()
{
for(int i=0;i<size;i++)
{
    int x=rand()%101;
a[i]=x;
}
}

void dispatch(int ch)
{
switch(ch)
{
    case 1:cout<<"Size of array :"<<getSize() << endl;
           break;
    case 2:fillWithRandom();
           break;
    case 3:    printTheArray();
           break;
    case 4:sort1();
           break;
    case 5:sort2();
           break;
    case 6:
        {
            cout<<"Enter the number you want to search\n";
                int t;
                cin>>t;
                int res1=sequentialSearch(t);
                if(res1!=-1)
                    cout<<"Found in position :"<<res1<<endl;
                else if(res1==-1)
                    cout<<"Not Found \n";
            break;
     }
    case 7:   
    {
        cout<<"Enter the number you want to search\n";
            int t;
            cin>>t;
            int res=binarySearch(t);
            if(res!=-1)
                cout<<"Found in position :"<<res<<endl;
            else if(res==-1)
                cout<<"Not Found \n";
            break;
    }
            default:cout<<"wrong choice\n";
}
}

int main ()
{

printMenu();
int choice;
cout<<"Type in a choice"<<endl;
cin>>choice;

while(choice!=0)
{
    dispatch(choice); // one big switch statement       
    printMenu();
    cout<<"Type in a choice"<<endl;
    cin>>choice;
}

cout << endl;


// wrap-up

cout << "This program is coded by Troy Wilms" << endl;  // fill in your name

// stops the program to view the output until you type any character
return 0;
}

1 个答案:

答案 0 :(得分:1)

size中的本地变量getSize()隐藏了永远不会被设置的全局变量size

printTheArray使用全局(未设置)size变量。

两种解决方案:

  1. 调用时size = getSize()确保设置了全局变量。
  2. (更好)只需使用size内的全局getSize()变量,即删除本地int size