为什么visual studio不运行这个程序 [英] Why visual studio doesnt run this program
问题描述
给定一组数字,包含10个元素。编写一个函数,按升序或降序对数组进行排序,具体取决于第三个函数参数。如果它等于1,则排序按降序执行,如果为0,那么它是升序。前两个2个函数参数是一个数组及其大小,第三个参数默认等于1.
单词default和homework下面有红线。(
我的尝试:
#include < iostream >
#include < ctime >
使用 命名空间 std;
int homework( int arr [], const int SIZE = 10 , int 默认 = 1 ){
srand(time(NULL));
for ( int i = 0 ; i< SIZE; i ++)
{
arr [i] = rand()% 100 ;
}
for ( int i = 0 ; i< SIZE; i ++)
{
cout<< arr [i]<< ;
}
cout<< endl<< ENDL;
if ( default == 1 ){
for ( int i = 0 ; i< SIZE; i ++)
{
for ( int i = 0 ; i< SIZE - 1 ; i ++){
if (arr [i]< arr [i + 1 ]){
int temp = arr [i];
arr [i] = arr [i + 1 ];
arr [i + 1 ] = temp;
}
}
}
}
其他 如果(默认!= 1 ){
for ( int i = 0 ; i< SIZE; i ++)
{
for ( int i = 0 ; i< SIZE - 1 ; i ++){
if (arr [i]> arr [i + 1 ]){
int temp = ARR [I];
arr [i] = arr [i + 1 ];
arr [i + 1 ] = temp;
}
}
}
}
for ( int i = 0 ; i< SIZE; i ++)
{
cout<< arr [i]<< ;
}
return 0 ;
}
int main(){
const int SIZE = 10 ;
int arr [SIZE];
作业(arr,SIZE, 1 );
}
你应该将函数头更改为:
int homework( int arr [], const int 大小, bool loopType)
你不能在实现中使用默认参数。不要使用BIG LETTER作为var名称,但仅适用于常量。并且默认是一个reserverd关键字(在交换机中)。
您的编码
if (默认 == 1 ){
// ...
}
其他 if (默认!= 1 )< span class =code-summarycomment> /// NOT NEEDED !!!!解析为此
if (loopType){
// ...
}
其他 {你可以在一个循环中完成< pre lang =c ++> for ( int i = 0 ; i< SIZE; i ++)
{
arr [i ] = rand()% 100 ; // 设定值
cout<< arr [i]<< ; // 读取和输出值
}
Given an array of numbers, containing 10 elements. Write a function that sorts an array in ascending or descending order, depending on the third function parameter. If it is equal to 1, sort is carried out in a descending order if 0 then it is a ascending order. First two 2 function parameters is an array and its size, the third parameter is default equal to 1.
There is red line under the word "default" and "homework".(
What I have tried:
#include<iostream>
#include<ctime>
using namespace std;
int homework(int arr[], const int SIZE = 10, int default = 1) {
srand(time(NULL));
for (int i = 0; i < SIZE; i++)
{
arr[i] = rand() % 100;
}
for (int i = 0; i < SIZE; i++)
{
cout << arr[i] << " ";
}
cout << endl << endl;
if (default == 1) {
for (int i = 0; i < SIZE; i++)
{
for (int i = 0; i < SIZE - 1; i++) {
if (arr[i] < arr[i + 1]) {
int temp = arr[i];
arr[i] = arr[i + 1];
arr[i + 1] = temp;
}
}
}
}
else if (default != 1) {
for (int i = 0; i < SIZE; i++)
{
for (int i = 0; i < SIZE - 1; i++) {
if (arr[i] > arr[i + 1]) {
int temp = arr[i];
arr[i] = arr[i + 1];
arr[i + 1] = temp;
}
}
}
}
for (int i = 0; i < SIZE; i++)
{
cout << arr[i] << " ";
}
return 0;
}
int main() {
const int SIZE = 10;
int arr[SIZE];
homework(arr, SIZE, 1);
}
you should change the function head to this:
int homework(int arr[], const int size, bool loopType)
you cant use default parameters in the implementation. Dont use BIG LETTER as var names, but only for constants. And "default" is an reserverd keyword (in switch loopes)
your coding
if (default == 1) { // ... } else if (default != 1)///NOT NEEDED !!!!resolves to this
if (loopType) { // ... } else {you can do it in one loop
for (int i = 0; i < SIZE; i++) { arr[i] = rand() % 100;// set value cout << arr[i] << " "; //read and output value }
这篇关于为什么visual studio不运行这个程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!