简体   繁体   中英

Adding the sum of positive and negative integer in the array c# for loop

The part ""The sum of positive input is:" and "The sum of negative input is:" are not working there's no answer showing what could be the solution? btw this an 11th grade question

int i, num, sum = 0;
Console.Write("\nInput the number of elements to be stored in the array :");
num = Convert.ToInt32(Console.ReadLine());

for (i = 0; i < num; i++)
{
    Console.Write("Elements - {0}:", i, num);
    array[i] = Convert.ToInt32(Console.ReadLine());
}
Console.Write("The positive inputs are :");
for (i = 0; i < num; i++)
{
    if (array[i] >= 0)
    {

        Console.Write(array[i] + ",");
        array[i]++;


    }
}
Console.Write("\nThe sum of positive input is :");
for (i = 0; i < num; i++)
{
    if (array[i] >= 0)
    {
        sum += array[i];
    }
}
    Console.Write("\nThe negative inputs are :");
for (i = 0; i < num; i++)
{
    if (array[i] < 0)
    {
        Console.Write(array[i] + ",");
        array[i]++;

    }
}
Console.Write("\nThe sum of negative input is :");
for (i = 0; i < num; i++)
{
    if (array[i] < 0)
    {
        sum += array[i];
    }
}

Special thanks to Sir Usama And Sir Drag and Drop

int[] array = new int[100];
int i, num, sum = 0;

Console.Write("\nInput the number of elements to be stored in the array :");
num = Convert.ToInt32(Console.ReadLine());

for (i = 0; i < num; i++)
{
    Console.Write("Elements - {0}:", i, num);
    array[i] = Convert.ToInt32(Console.ReadLine());
}

Console.Write("The positive inputs are :");
for (i = 0; i < num; i++)
{
    if (array[i] >= 0)
    {
        Console.Write(array[i] + ",");
    }
}

Console.Write("\nThe sum of positive input is :" );
for (i = 0; i < num; i++)
{
    if (array[i] >= 0)
    {
        sum += array[i];
    }
}

Console.Write(sum);
sum = 0;
Console.Write("\nThe negative inputs are :");
for (i = 0; i < num; i++)
{
    if (array[i] < 0)
    {
        Console.Write(array[i] + ",");
    }
}

Console.Write("\nThe sum of negative input are :" );
for (i = 0; i < num; i++)
{
    if (array[i] < 0)
    {
        sum += array[i];
    }
}

Console.Write(sum);

First, you don't initialize your array after you read the array length from user input. You should have:

int[] array;
Console.Write("\nInput the number of elements to be stored in the array :");
num = Convert.ToInt32(Console.ReadLine());
array = new int[num];

Second, you are using the same sum variable for both positive and negative calculation (without resetting it in between). You can use two separate variables for these two computations:

int i, num, posSum = 0, negSum = 0;

Third, as Drag and Drop mentioned in the comment, you don't need that array[i]++; , since it modifies the elements of the array.

Fourth, as Usama Aziz mentioned in the comment, you don't have a Console.Write where you print the results.

The final solution should look like this:

static void Main(string[] args)
{
    int i, num, posSum = 0, negSum = 0;
    int[] array;
    Console.Write("\nInput the number of elements to be stored in the array :");
    num = Convert.ToInt32(Console.ReadLine());
    array = new int[num];

    for (i = 0; i < num; i++)
    {
        Console.Write("Elements - {0}:", i, num);
        array[i] = Convert.ToInt32(Console.ReadLine());
    }

    Console.Write("The positive inputs are :");
    for (i = 0; i < num; i++)
    {
        if (array[i] >= 0)
        {
            Console.Write(array[i] + ",");
        }
    }

    Console.Write("\nThe sum of positive input is :");
    for (i = 0; i < num; i++)
    {
        if (array[i] >= 0)
        {
            posSum += array[i];
        }
    }
    Console.Write(posSum);

    Console.Write("\nThe negative inputs are :");
    for (i = 0; i < num; i++)
    {
        if (array[i] < 0)
        {
            Console.Write(array[i] + ",");
        }
    }

    Console.Write("\nThe sum of negative input is :");
    for (i = 0; i < num; i++)
    {
        if (array[i] < 0)
        {
            negSum += array[i];
        }
    }
    Console.Write(negSum);
}

Good luck on your exam!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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