[英]Take an array of numbers and then display only the distinct number
In the below code I am taking an array to read 4 numbers and I need only to display the distinct number. 在下面的代码中,我将读取一个4个数字的数组,并且只需要显示不同的数字即可。
#include <stdio.h>
int main(){
int ch[3];
int s[3];
int count = 0;
int i;
int j;
for(i = 0; i < 4; i++){
scanf("%d", &s[i]);
for(i = 0; i < 4; i++){
ch[i] = s[i];
printf("ch= %d", ch[j]);
}
if(ch[i] == s[i]){
count = count + 1;
printf("%d =", count);
}
}
}
Somewhat I'm not getting the output, it is giving me a weird output. 我有点没得到输出,这给了我一个奇怪的输出。
What could be the cause of the strange output? 产生奇怪输出的原因可能是什么?
int ch[3];
int s[3];
This mean valid references to these arrays can be made from 0 to 2 , whereas your for loop is looping 4 times. 这意味着对这些数组的有效引用可以从0到2进行,而您的for循环将循环4次。
printf("ch= %d",ch[j]);
you mean ch[i] ? 你是说ch [i]吗?
what do you use "int j" for ? 您将“ int j”用作什么?
Cant understand the question but i wrote a code for taking 4 inputs and giving the distinct number among them as an output(or the numbers which have only occurred once). 不能理解这个问题,但是我编写了一个代码,接受4个输入,并给出其中的不同数字作为输出(或仅发生一次的数字)。
#include <stdio.h>
int main(){
int ch[4];
int s[4];
int i;
int j;
for(j=0;j<4;j++){
ch[j]=0;
}
for( i=0;i<4;i++){
scanf("%d",&s[i]);
}
for(i=0;i<4;i++){
for(j=0;j<4;j++){
if(s[i]==s[j]){
ch[i]++;
}
}
}
for(i=0;i<4;i++){
if(ch[i]==1){
printf("\n%d is the distinct number.", s[i]);
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.