如何通過放入 scanf 的數字數量來定義數組的限制?

SajLencV02

例如:輸入:420 50 -4 輸出:數字 3 正 2 負 1

同樣的代碼:輸入:420 50 -4 7 輸出:數字 4 正 3 負 1在此處輸入圖像描述

   #include<stdio.h>
 #define N 2 
int main()
{

 int a[N], i=0, n=0, k=0, z=0; 

for(i=0; i<N; i++)
 {

 scanf("%d" , &a[i]);

    if((a[i] >= -10000 && a[i] <= 10000 ))
    n++;
    if(a[i]>0)
    k++;
    if(a[i]<0)
    z++;
 }

 printf("Numbers:%d \n", n);
 printf("Positive:%d \n", k);
 printf("Negative:%d \n", z);

return 0;
}
矢野

此示例允許用戶“無限期”輸入數字,而無需提示輸入多少。當然,你的電腦只有這麼多內存,所以有一個限制,但沒有實際限制。本質上,您需要選擇一個初始大小,然後在達到該大小時動態分配更多空間。

#include <stdio.h>
#include <stdlib.h>

#define INITIAL_SIZE 10

void printArray(const int* myArray, size_t numsEntered)
{
    for (size_t i=0; i<numsEntered; i++)
    {
        printf("myArray[%zu] = %d\n", i, myArray[i]);
    }
}

int main(void)
{
    size_t arraySize = INITIAL_SIZE;
    size_t numsEnteredSoFar = 0;
    int* myArray = malloc(sizeof(*myArray) * arraySize);  // initially make room for 10
    if (myArray == NULL) exit(-1);  // whoops, malloc failed, handle this error how you want

    while(1)
    {
        int curEntry;
        printf("enter a number, or 'q' to quit: ");
        if (scanf("%d", &curEntry) == 1)
        {
            // store in the array, increment number of entries
            myArray[numsEnteredSoFar++] = curEntry;

            // here you can check for positives and negatives, or
            // wait to do that at the end. The point of this example
            // is to show how to dynamically increase memory allocation
            // during runtime.

            if (numsEnteredSoFar == arraySize)
            {
                puts("Array limit reached, reallocing");
                // we've reached our limit, need to allocate more memory to continue.
                // The expansion strategy is up to you, I'll just continue to add
                // INITIAL_SIZE
                arraySize += INITIAL_SIZE;
                int* temp = realloc(myArray, arraySize * sizeof(*myArray));
                if (temp == NULL)
                {
                    // uh oh, out of memory, handle this error as you want. I'll just
                    // print an error and bomb out
                    fprintf(stderr, "out of memory\n");
                    exit(-1);
                }
                else
                {
                    // realloc succeeded, we can now safely assign temp to our main array
                    myArray = temp;
                }
            }
        }
        else
        {
            // the user entered 'q' (or anything else that didn't match an int), we're done
            break;
        }
    }

    // print the array just to show it worked. Instead, here you can
    // loop through and do your comparisons for positive and negative,
    // or you can continue to track that after each entry as you've
    // shown in your code
    printArray(myArray, numsEnteredSoFar);

    free(myArray);
    return 0;
}

演示

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何通過將數字列表中的數字相加來找到給定的數字並返回使用過的數字?

如何創建一個java程序來查找數組中連續數字的數量?

如何通過對多個變量進行分組來創建新的 Pandas 數據框?

如何使用拆分方法來解析這組數字?

如何通過在每個數組的末尾放置一個來連接兩個數組?

如何通過比較其他兩個文檔的數組來查找文檔

如何通過匹配 mongodb 中的 id 來合併數組字段?

如何通過對數據進行分組來返回唯一的行

如何通過在包含與字段匹配的數組的對象的數組上運行一次來創建 2 個數組?

如何通過powershell或cmd計算屏幕數量

如何通過另一個數組編號鍵過濾數組鍵

如何在函數定義中使用變量的值作為關鍵字?

如何通過數組列表將變量發送到方法

僅當高於 minimum_range 時,我如何才能通過讓他輸入數字來限制用戶?

如何通過二維數組索引使用?

如何在 C 中使用 scanf() 檢查輸入字符數組 (%s) 的長度

通過根據數據框中的數字添加行和值來擴展數據框

如何定義一個數組的自定義 TypeScript 定義,其中除第一個之外的所有元素都是數字

通過與另一個數組中的值進行比較來過濾嵌套數組

在 Python 中,如何在函數中定義任意數量的索引變量?

如何通過基於行值創建新變量來重塑數據框以進行分析

如何使用字符串數組來過濾對像數組?

如何通過在 PySpark 中選擇 struct-array 列的一個字段來提取數組列

如何通過添加匹配對像作為新字段來合併兩個對像數組

如何通過在js中使用reduce返回數組中的對象來創建嵌套對象

如何在數組中創建 React 元素(使用 props),然後通過簡單地映射到該數組來渲染它們?

如何通過另一個數組屬性組合和過濾數組屬性

java - 如何通過更改數組y的值將數組x更改為數組y的值?

如何解析數組的JSON數組並通過名稱引用內部數組的成員?