首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >为什么c++中数组定义的位置会引发错误?

为什么c++中数组定义的位置会引发错误?

提问于 2022-11-29 20:58:49
回答 1关注 0查看 111

如图,我在做csp一道编程题的时候,定义的数组

代码语言:cpp
复制
#include<bits/stdc++.h>
using namespace std;

int main(){
	
	int a[1001];//此处是我定义的数组 
	int n;
	cin>>n;
	for(int i=0;i<n;i++){
		int temp;
		cin>>temp;
		a[temp]++;
		cout<<a[temp]<<" ";
	}
	return 0;
} 

得到了这样的结果,请问是怎么回事呢?

代码改成这样就没错了

代码语言:cpp
复制
#include<bits/stdc++.h>
using namespace std;

nt a[1001];//此处是我定义的数组 
int main(){
	
	i
	int n;
	cin>>n;
	for(int i=0;i<n;i++){
		int temp;
		cin>>temp;
		a[temp]++;
		cout<<a[temp]<<" ";
	}
	return 0;
} 

结果

回答

和开发者交流更多问题细节吧,去 写回答
相关文章

相似问题

相关问答用户
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档