简体   繁体   English

在C中,我的程序输出的数字不在用户输入数组内或数组中任何数字之和不输出任何内容

[英]In C, my program to output a number not inside a user input array or not sum of any numbers in the array is not printing anything

I am doing an assignment for class. 我正在上课。 The prompt is: 提示是:

Write a program to find the smallest positive integer that does not appear in the array. 编写程序以查找未出现在数组中的最小正整数。 The number can't be formed by sum of 2 different or sum of same numbers from the array. 该数字不能由数组中2个不同或相同数字的总和形成。 for e1,2,3 and 4 appear in this array. e1,2,3和4出现在此数组中。 4 can be formed as 3+1, 5 as 3+2, 6 as 3+3 , 7 as 3+4 and 8 as 4+4 (it is ok to use a number twice). 4可以形成为3 + 1,5可以形成3 + 2,6可以形成3 + 3,7可以形成3 + 4,8可以形成4 + 4(可以使用两次数字是可以的)。 9 does not appear in the array and it can not be formed as sum of 2 numbers in the array. 9没有出现在数组中,并且不能形成为数组中2个数字的和。 So, 9 is the solution for this array. 因此,9是该数组的解决方案。 Use functions 使用功能

 int issumof2(int data[], int size, int number) int inarray(int data[], int size, int number) 
  • issumof2 returns 1 if number is sum of 2 elements in the data and returns 0 otherwise. 如果数字是数据中2个元素的和,则issumof2返回1,否则返回0。
  • inarray returns 1 if the number is in the data and returns 0 otherwise. 如果数据中包含数字,则inarray返回1,否则返回0。
#include <stdio.h>
//function declaration
int issumof2(int data[], int size, int number);
int inarray(int data[], int size, int number);
int size = 7, sum2, sum, number, data[7], i, j;

int main()
{
    //get user input
    printf("Enter 7 numbers: ");
    for (i = 0; i < size; i++)
        scanf("%d", &data[i]);  
    //function call
    issumof2(data, size, number);
    inarray(data, size, number);
    number = 0;
    //loop while condition is true and stop when condition becomes false
    while (inarray(data, size, number) == 1 || issumof2(data, size, number) == 1)
    {
        number = number + 1;//increment number till loop stop
         //print smallest number
        printf("Smallest positive integer: %d\n", number);
    }
    return 0;
}
Expected output:
Enter 7 numbers
1 2 2 3 4 3 1
Smallest positive Integer = 9
//1,2,3,4 are in array. 3+1=4,3+2=5,3+3=6,3+4=7,4+4=8(number can be used twice).
//9 is not inarray or not a  sum of 2 numbers in the array
int issumof2(int data[], int size, int number)
{
    //add data numbers in array
    for (i = 0; i < size; i++)
    {
        for (j = 0 + i; j < size; j++)
        {
            sum = data[i] + data[i];//add same numbers        
            sum2 = data[i] + data[j];//add all 2 combinations of different number             
            if (sum == data[i] || sum2 == data[i])//comparing with data value          
                return 1;//if sum  or sum2 exists in data
        }
    }
    return 0;//if sum or sum2 don't exist
}
//goes to infinite loop
int inarray(int data[], int size, int number)
{
    for (number = 1; number <=size; number++)
    {
        printf("inarray number=%d\n", number);
        //loop through data array
        for (i = 0; i < size; i++)
        {
            printf("data[i]=%d\n", data[i]);
            if (number == data[i])//if this is true it goes to infinite loop
                                  //if false it just stops comparing
            {
                return 1;
                printf("\n");
            }
        }
    }
    return 0;
}

A quick run of what you've posted actually results in an infinite loop, unless i've missed something. 快速浏览您发布的内容实际上会导致无限循环,除非我错过了一些东西。 A couple of errors I see: 我看到几个错误:

  • You have set size equal to three, but then use it to attempt to scan seven numbers. 您已将size设置size等于三,但是然后使用它来尝试扫描七个数字。
  • In both of the functions inarray and issumof2 , there are conditionals that return either one or zero depending on the result of the conditional, so they only ever run one iteration of the innermost loops. inarrayissumof2这两个函数中,有条件条件根据条件的结果返回一或零,因此它们只运行最内层循环的一次迭代。 You need to move the else statements outside of the loops. 您需要将else语句移出循环。
  • The smallest possible integer that meets the criteria given the sample input is in fact eight. 在给定样本输入的情况下,满足标准的最小可能整数实际上是8。
  • It seems the logic in both functions are off as they never return 1. 似乎这两个函数的逻辑都已关闭,因为它们从不返回1。
    • In issumof2 there is no use of number , yet that is what you must compare any sums to if I am not mistaken. issumof2 ,没有使用number ,但是如果我没有记错的话,那就是必须将任何总和进行比较的地方。 Additionally, i think you may have inadvertently used i in place of j or vice versa. 另外,我认为您可能无意中使用了i来代替j ,反之亦然。

Here is some of your code commented with remarks that hopefully lead you to fix the mistakes: 这是您的一些代码中带有注释的注释,它们有望使您解决错误:

int main() {
    int data[7];
    int i, number = 0;

    // size is three so we won't scan 7 integers if we enter them line by line.
    printf("Enter 7 numbers: ");
    for (i = 0; i < size; i++) 
        scanf("%d", &data[i]);

    // the problem states that both of these functions must return zero for the
    // expected answer, yet this loop breaks whenever number does not meet both
    // criteria, which isn't what we want.
    while(inarray(data, size, number) == 0 && issumof2(data, size, number) == 0) {
        number = number + 1;
        // if the conditional matched the problem statement, then we would print
        // this line for every integer that fails the criteria, but that isn't
        // what we want.
        printf("Smallest positive integer: %d\n", number);
    }
    return 0;
}

int issumof2(int data[], int size, int number) {
    int i, j, sum = 0;
    for (i = 0; i < size; i++) {
        // here we've doubled the value at i and saved within a temporary
        // variable -- but why? 
        sum2 =data[i]+data[i];
        for (j = 0; j < size; j++) {
            // here we're combining different values to test their sum against
            // number -- good job 
            sum = data[i] + data[j];
            // why are we comparing the data at i with twice itself (sum2), and
            // its addition with the data at j (sum)? we must compare sum2
            // against number.
            if (sum==data[i] || sum2==data[i])
                // here we return 1 if the condition is met. this would be good
                // if our conditional was representative of the problem.
                return 1;
            else
                // otherwise we return 0 -- why? i guess we don't need to check
                // any other sums...
                return 0;
        }
    }
    // alright we're done here. or are we? the prototype says we'll return an int
}

int inarray(int data[], int size, int number)
{
    int i;
    // so we're looping over all input -- good job
    for (i = 0; i < size; i++) {
        // here we test for equality of index i with the data at index i -- why?
        // we must compare the data at index i with number.
        if (i == data[i])
            return 1;
        else // again we can't just call it a day after checking just one number
            return 0;
    }
    // and as before we need to return some value if we complete the loop
}

And here is a corrected solution: 这是一个更正的解决方案:

#include<stdio.h>

int issumof2(int[], int, int);
int inarray(int[], int, int);

int size = 7;

int issumof2(int data[], int size, int number) {
    int i, j;
    for (i = 0; i < size; i++) {
        for (j = 0; j < size; j++) {
            if (i == j)
               continue;
           if (data[i] + data[j] == number)
               return 1;
        }
    }
    return 0;
}

int inarray(int data[], int size, int number) {
    int i;
    for (i = 0; i < size; i++) {
        if (data[i] == number)
            return 1;
    }
    return 0;
}

int main() {
    int data[7];
    int i, number = 1;
    printf("Enter 7 numbers: ");
    for (i = 0; i < size; i++)
        scanf("%d", &data[i]);

    while(inarray(data, size, number) == 1 || issumof2(data, size, number) == 1)
        number++;

    printf("Smallest positive integer: %d\n", number);

    return 0;
}

First Here is the corrected answer for your logic 首先,这是您逻辑的正确答案

#include<stdio.h>

int issumof2(int data[], int size, int number);
int inarray(int data[], int size, int number);
int size=7,sum2;
int main()
{
   int data[7];
   int i, number=1;
   printf("Enter 7 numbers: ");
   for (i = 0; i < size; i++)
       scanf("%d", &data[i]);
   //I am a little confused about this part. I wasn't sure how to write it.
   while(inarray(data, size, number)==1 || issumof2(data, size, number)==1)//till functions are false
   {
        number=number+1;
   }

    printf("Smallest positive integer: %d\n", number);                      
    return 0;
}

int issumof2(int data[], int size, int number)
{
   int i, j, sum = 0;
   for (i = 0; i < size; i++)
   {
        sum2 =data[i]+data[i];
       for (j = 0; j < size; j++)
       {
           sum = data[i] + data[j];           
           if (sum==number || sum2==number) {
           return 1;

       }
       }

   }

   return 0;
}

int inarray(int data[], int size, int number)
{

   int i;
   for (i = 0; i < size; i++)
   {
       if (number == data[i]) {
           return 1;
       }
    }

   return 0;
}

Also I have prepared one more set of code explaining your mistakes. 另外,我还准备了一套解释您的错误的代码。 Hope it helps for you to learn 希望对您有帮助

#include<stdio.h>

int issumof2(int data[], int size, int number);
int inarray(int data[], int size, int number);
int size=7,sum2; // Size has be changed from 3 to 7 , since you want to check the for 7 numbers

int main()
{
   int data[7];
   int i, number=0;
   printf("Enter 7 numbers: ");
   for (i = 0; i < size; i++)
       scanf("%d", &data[i]);
   //I am a little confused about this part. I wasn't sure how to write it.
//   while(inarray(data, size, number)==0 && issumof2(data, size, number)==0)//till functions are false 

//   I feel its best to check the followin way
//   If any one of the condition is true, then that is not the required number, so increment it
//   else leave the loop which is the required number
   while(inarray(data, size, number)==1 || issumof2(data, size, number)==1)//till functions are false 
    {
        number=number+1;
        //printf("Smallest positive integer: %d\n", number);   
        // You will print the required number after the loop, not inside the loop

    }
        printf("Smallest positive integer: %d\n", number);                      
    return 0;
}

int issumof2(int data[], int size, int number)
{
   int i, j, sum = 0;
   for (i = 0; i < size; i++)
   {
        sum2 =data[i]+data[i];
       for (j = 0; j < size; j++)
       {
           sum = data[i] + data[j];           
//           if (sum==data[i] || sum2==data[i]) 
           if (sum==number || sum2==number)  // You are supposed to check the Sum with Number. Not with data irself
               return 1;
//            else
//                return 0; // Returning 0 here is not correct, It should be returned if all the condition are failed. 
       }
   }

   return 0; // Correct place to return 0;
}

int inarray(int data[], int size, int number)
{

   int i;
   for (i = 0; i < size; i++)
   {
//       if (i == data[i])
       if (i == number) // You are supposed to check with numbner not with data itself
           return 1;
//       else 
 //           return 0; // You are not supposed to return 0 here. Return is after all the condition is failed
    }

   return 0; // Corect place to return 0
}

I compiled and ran your code. 我编译并运行了您的代码。 It will print, but runs off into the hundreds of thousands. 它会打印,但会成千上万。

$ gcc code.c
$ ./a.out
>Enter 7 numbers: 1 2 3 4 5 6 7 --press enter here--
...

I could somewhat recreate your issue if I pressed enter after a single number. 如果我在输入单个数字后按Enter,则可以稍微重现您的问题。 Make sure you're typing your input as a single line. 确保您输入的内容是一行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM