我该如何将这行代码放到这个程序中 [英] how do I put this lines of code to this program

查看:70
本文介绍了我该如何将这行代码放到这个程序中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#include<iostream>
#include<math.h>
#define _USE_MATH_DEFINES // for C++
#include <cmath>
using namespace std;

/*functions used for Selection Sort Program */
void shiftArray(int* anArray, int aStart, int anEnd);
int getLargestInArray(int * anArray, int aSize);
void selectionSort(int* anArray, int aSize);
void printArray(int* anArray, int aSize);

int main(){
	int iChoice;	
	cout<<"CHOOSE A PROGRAM:";
	cout<<"\nSelection Sort[1] \nPalindrome[2] \nPi Table[3]\n ";
	cin >> iChoice;
	
	if(iChoice==1){
	system("pause");
	/* insert SELECTION SORT PROGRAM HERE*/
	}
	
	
else if(iChoice==2){

	int iInput;
		cout <<"ENTER A NUMBER:";
		cin >> iInput;
	
	int iCopyinp, iCopy,iCount=0;
	iCopyinp=iInput;
	iCopy=iInput;
	
	while(iInput!=0){
		iCount++;
		iInput = iInput/10;
	}
	
	int iMulti=1;
	while(iCount!=1){
		iMulti= iMulti *10;
		iCount--;
	}
	
	int iSum=0;
	while(iMulti!=0){
		iSum = iSum + (iMulti * (iCopy % 10));
		iMulti = iMulti / 10;
		iCopy = iCopy /10;
	}
	
	if(iSum==iCopyinp)
	cout <<"\nPALINDROME";
	else
	cout <<"\nNOT A PALINDROME";
	}
	
	
	else if(iChoice==3){
	int  iTerm;
	float  iSum=0.00,iDenom=1.00,iError;
	float iPi =  M_PI;
/*	
cout<<"| Term | Value | Error " ;*/

	for (iTerm = 1; iTerm <= 100; iTerm++){
 
	if (iTerm % 2)
		iSum += (4 / iDenom);
	else
		iSum -= (4 / iDenom);
	
	cout << "|" << iTerm << "|" << iSum;
	iError = iSum - iPi;
	cout << "|" << iError << endl;
	cout << "\n";
	iDenom = iDenom + 2;
}
}
	else
	return 0;
	}





上面的代码是我需要提交给老师的代码。我们的任务是制作3个程序,并将这三个程序合并为一个程序。不幸的是,我只能把2个程序放进去。另一个包含错误,如果我转移它。有人能帮我吗?你能解释为什么会有错误。



这是我需要插入的代码。这个代码可以运行,但是当我将代码转移到上面的程序中时,它不再起作用了,我不理解错误:(



SELECTION SORT PROGRAM :





the code above is the one I need to submit to my teacher. we were tasked to make 3 programs and combine this three programs into one program. unfortunately, I was only able to put 2 programs inside. the other one contains errors if I transfer it. Can someone help me? can you explain why there is an error.

this is the code I need to insert. this code works if you run it but when I transfer the codes into the program above, it doesnt work anymore and I do not understand the error :(

SELECTION SORT PROGRAM:

#include <iostream>

using namespace std;

void shiftArray(int* anArray, int aStart, int anEnd);
int getLargestInArray(int * anArray, int aSize);
void selectionSort(int* anArray, int aSize);


void printArray(int* anArray, int aSize){
	cout<<"\n";
	for (int i=aSize-1; i>=0 ; i--){
    cout<<anArray[i]<<endl;
  }
  
}


int getLargestInArray(int* anArray, int aSize){
  int iHighest = 0;
  for(int i=0; i<asize;>    if(anArray[iHighest]<anarray[i]){>
      iHighest =i;
    }
  }

  return iHighest;
}


void selectionSort(int* anArray, int aSize){
  int iPos, iKey;
  for(int i = aSize; i>=0; i--){
    iPos = getLargestInArray(anArray, i);
    iKey = anArray[iPos];
    //cout << iKey;
    
    shiftArray(anArray, iPos, i);
    anArray[i - 1] = iKey;
  }


}
void shiftArray(int* anArray, int aStart, int anEnd){
  for(int i=aStart; i<anend;>    anArray[i] = anArray[i+1];
  }
}


int main(){
	  int iInput,iCount=0;
	  cout<<"HOW MANY NUMBERS:" ;
	  cin >> iInput;
	  int array[iInput];
	  
	  while(iCount<iinput){>
	  		cin >> array[iCount];
	  		iCount++;
	  }
	  
	  selectionSort(array, iCount);
	  printArray(array, iCount);
	  return 0;
}







推荐答案

只能在同一范围内使用一次函数名。 (如果函数没有重载)。



你必须重命名一些函数和变量。



更多准确地说明错误并在将来复制完整的信息(或第一行)! :-O



PS:阅读一些关于编程的基本教程。
you can only use a function name once in the same scope. (If the functions arent overloaded).

You must rename some functions and variables.

Be more precise about the errors and copy the full message (or first line) in the future! :-O

PS: Read some basic tutorials about programming.


哦我终于解决了。哈哈哈哈。感谢KarstenK的帮助。
oh I finally solved it. hahahaha. thanks for the help KarstenK.


这篇关于我该如何将这行代码放到这个程序中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆