[英]scanf is continuously taking values (input)
I have a homework problem where I have to input an array, and take out all the distinct elements from it. 我有一个作业问题,必须输入一个数组,并从中取出所有不同的元素。
For this I have made a new sub_array where all distinct values will be stored, Howsoever during input scanf
is not taking values as supposed to? 为此,我创建了一个新的sub_array,其中将存储所有不同的值,但是在输入
scanf
期间却没有按预期方式获取值?
Here is my code:- 这是我的代码:
# include <stdio.h>
# include <stdlib.h>
int main()
{
int number; // Variable name "number" which will specify the size of dynamically allocated array.
printf("Enter the size of your array\n");
scanf("%d",&number);
int *array;
array = (int*)calloc(number, sizeof(int)); // Dynamically allocated memory.
int i,j=0; // Counter variables for loop.
printf("Enter the elements of arrays\n");
for(i = 0; i < number; i++)
{
scanf("%d",(array + i)); // Code is asking for endless numbers and is not outputting the desired result. THIS IS THE PROBLEM SECTION.
}
for(i = 0; i < number; i++)
{
for( j = i + 1 ; j < number; j++)
{
if( *(array + i ) == *(array + j))
{
*(array + j) = 0; // My way of removing those numbers which are the repeated. (Assigning them value of zero).
}
}
}
i=0;j=0;
int sub_number = 0;
while(i < number)
{
if(*(array + i) != 0)
{
sub_number++; // Variable name "sub_number" which will specify the size of dynamically allocated array "sub_array".
}
}
int *sub_array;
sub_array = (int*)calloc(sub_number,sizeof(int));
for(i = 0;i < sub_number;i++)
{
printf("%d\n",*(sub_array + i)); // Desired array which only contains distinct and unique variables.
}
return 0;
}
Edit:- I missed one loop where i forgot to fill the sub_array loop. 编辑:-我错过了一个循环,我忘了填写sub_array循环。 Here is the code :-
这是代码:-
for(i = 0;i < number ;i++) //New code inserted.
{
if( *(array + i ) != 0)
{
*(sub_array + j) = * (array + i );
j++;
}
}
Howsoever my program is still not working. 但是我的程序仍然无法正常工作。
In the following part of your code: 在代码的以下部分中:
while(i < number)
{
if(*(array + i) != 0)
{
sub_number++; // Variable name "sub_number" which will specify the size of dynamically allocated array "sub_array".
}
}
You are not incrementing i. 您没有增加i。 You miss an
i++;
您错过了
i++;
. 。 Your scanf cycle is correct.
您的scanf周期是正确的。
There are two errors in your program. 您的程序中有两个错误。
The first one is that inside the while loop the variable i is not incremented. 第一个是在while循环中变量i不增加。
Write the loop like 像这样写循环
while(i < number)
{
if(*(array + i++) != 0)
{
sub_number++; // Variable name "sub_number" which will specify the size of dynamically allocated array "sub_array".
}
}
The second one is that you forgot to copy elements from array to sub_array. 第二个是您忘记将元素从array复制到sub_array。
Include this loop 包括这个循环
for ( i = 0, j = 0; i < number; i++ )
{
if ( *( array + i ) != 0 ) *( sub_array + j++ ) = *( array + i );
}
before printing sub_array. 在打印sub_array之前。
And you should free all allocated memory. 并且您应该释放所有分配的内存。
free( sub_array );
free( array );
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.