c++ - PrintTheArray 函数不打印

标签 c++ arrays function

基本上我有这个程序打印一个菜单,并调用各种函数来对数组做不同的事情。我需要使用 getSize 来声明数组的大小,但我的老师不想在 main 函数中添加任何其他内容。当我使用 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;
}

最佳答案

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

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

两种解决方案:

  1. 调用时执行 size = getSize() 以确保设置了全局变量。
  2. (更好)只需在 getSize() 中使用全局 size 变量,即删除局部 int size

关于c++ - PrintTheArray 函数不打印,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47255327/

相关文章:

c++ - 如何设置 QT 调试器以提供更有意义的消息?

Java读取文本文件2列并存储在数组中

javascript - Javascript 中是否可以为匿名调用函数提供参数计数?

function - 如何在 Dart 中的数值数据表值之间进行插值(实际上是任何语言)

c++ - OpenGL 不绘制到 SDL2 窗口

c++ - 异步 lib​​pcap : losing packets?

c++ - 链接库时未定义对 `boost::filesystem::path_traits::dispatch 的引用?

javascript - For循环转换一系列变量

javascript - 在 Javascript 中交换整个数组

c++ - C++ 函数中的消息传递