2017-11-12 9 views
0

기본적으로이 프로그램은 메뉴를 인쇄하고 다양한 기능을 호출하여 배열에 다른 작업을 수행합니다. getSize를 사용하여 배열의 크기를 선언해야하지만, 선생님은 main 함수에서 다른 것을 원하지 않습니다. getSize를 사용할 때 작동하는 것처럼 보이지만 PrintTheArray는 아무 것도 인쇄하지 못합니다 ... 제안? 형식 지정에 대해서도 유감입니다. 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; 
} 

답변

2

지역 변수 size 설정 결코 극복 전역 변수 size을 숨 깁니다.

printTheArray은 전역 변수 (설정되지 않음) size을 사용합니다.

두 솔루션 : 전역 변수를 확인하기 위해 호출 할 때

  1. size = getSize()를 수행가 설정됩니다.
  2. (더 나은 점) 내부의 변수 size을 사용하면 즉 int size 로컬을 제거하십시오.
+0

효과가 있습니다. 방금 전역 변수를 0으로 설정하고 getSize에서 로컬 변수를 제거했습니다. 생명의 은인! – Wahremacht

관련 문제