简体   繁体   English

为什么即使我用 C 语言扫描小数字,我的数组也会打印大数字

[英]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.

相关问题 为什么在 c++ 中添加数组元素时我会得到非常大的数字? - Why am i getting exremely large numbers when adding elements of an array in c++? 我的所有属性均为零,即使我用数字和对象填充了它们,数组也为零 - All my properties are zero, and my array is zero, even though I have populated them with numbers and objects 为什么我从数组C ++中得到这么多数字 - why am i getting so many numbers from my array c++ 为什么我在C中获得这么大的数字? - Why am I getting this huge numbers in C? 我正在尝试反转 C 中的数组。 但是,代码将随机 1-2 抛出到一些非常大的数字 - I am trying to reverse an array in C. But, the code throws out random 1-2 to some very large numbers digit numbers 我在为包含整数1-25的数组创建循环并在每行打印5个数字时遇到问题 - I am having problems creating a loop for an array that contains the integers 1-25 and prints 5 numbers per line 为什么我会从二维数组中打印出随机数? - Why am I getting random numbers being printed from my 2D array? 为什么即使我将它转换为数组,我的 querySelectorAll 也不起作用? - Why is my querySelectorAll not working even though I converted it to an array? 为什么我在输出中得到随机数? - Why am I getting random numbers in my output? 扫描二维数组以查找C中的质数 - Scanning a 2d array for prime numbers in C
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM