[英]Why I my array prints large numbers even though i am scanning the small numbers in C language
i am trying to print the array with no duplicate numbers inside that, for that i have to take 20 numbers from users between the range of 10 and 100. There is a condition that i have to use only one array to solve this problem.我正在尝试打印其中没有重复数字的数组,为此我必须从 10 到 100 范围内的用户那里获取 20 个数字。有一个条件是我必须只使用一个数组来解决这个问题。
Problem is when I am trying to enter the number such as 99, 98 etc the array prints those numbers but it also prints the large random numbers i don't know what problem is there in my code.问题是当我尝试输入 99、98 等数字时,数组会打印这些数字,但它也会打印大随机数,我不知道我的代码中有什么问题。 Can anyone help me with this.谁能帮我这个。
#include<stdio.h>
#include<stdlib.h>
#define MAX 20
void main(){
int number_array[MAX]; //the only array which will store the non repitative value
int i,j,k=0,duplicate;
long value; //having duplicate which will catch the duplicate value
printf("Enter 20 numbers between 10 and 100: \n");
//read the numbers from using the for loop
for(i=0; i < MAX - 1; i++){
duplicate = 0;
scanf("%ld",&value);
if(value <= 10 || value >= 100) { //if value not lies in range ask again to read the input
printf("Please enter the value between 10 and 100\n");
i--;
}
for(j = 0; j < k; j++){ //checks whether the read value already stored in array
if(value == number_array[j]){ // if yes break the loop
duplicate = 1;
break;
}
}
if(!duplicate && (value > 10 && value < 100)){ //this conditio checks the range and duplicacy if yes do not stored in array
number_array[k++] = value;
}
}
printf("The Array with no repitive integers are: \n");
printf("[");
for(i = 0; i < sizeof(number_array); i++){
if(i == sizeof(number_array) - 1){
printf("%d ", number_array[i]);
}else {
printf("%d, ", number_array[i]);}
}
printf("]");}
Here is the output:这是 output:
Enter 20 numbers between 10 and 100:
21
23
24
25
26
27
8
Please enter the value between 10 and 100
29
28
28
98
97
96
95
94
92
89
78
54
45
The Array with no repitive integers are:
[21, 23, 24, 25, 26, 27, 29, 28, 98, 97, 96, 95, 94, 92, 89, 78, 54, 45, 6422352, 4201163, 0, 18, 17, 23, 6422284, 3584000, 6422352, 4199048, 1, 12201480, 12197232, 2, 0, 6422352, 2002089485, 3584000, 6422400, 4199157, 1, 0, 0, 0, 0, 0, 0, 2004247385, 3584000, 2004247360, 6422492, 2011597124, 3584000, 811369607, 0, 0, 3584000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 6422412, 0, 6422500, 2011672880, 1204624915, 0, 6422508, 2011597076, -1, 2011734250, 0, 0, 4199136, 3584000, 0 ]
----jGRASP wedge2: exit code for process is 93.
----jGRASP: operation complete.
for(i = 0; i < sizeof(number_array); i++){
You don't care how many characters number_array
takes up in memory.您不在乎 number_array 在number_array
中占用了多少个字符。 So don't use sizeof
.所以不要使用sizeof
。 You care how many entries are in the array.您关心数组中有多少条目。 That's stored in k
.这存储在k
中。 So change sizeof(number_array)
to k
.所以将sizeof(number_array)
更改为k
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.