Finding the Average in a sequence of number in c while ignoring the negative numbers in the sequence

program to find average of n numbers in python
c program to find positive or negative number in array
c program to count positive and negative numbers using while loop
average of numbers in c
c++ program to find average of n numbers using while loop
how to find the average of 3 numbers in python
c program to find average of 5 numbers
c program to find average of n numbers using functions

I have to write a C program that reads a sequence of integers (positive, negative, or zero) and calculate the average of the positive integers only. If there are no positive numbers, you should display the following statement followed by a new line No positive numbers!

Here is my code, I just need help with how to ignore the negative numbers in the input sequence.

#include<stdio.h>

int main(){

    int num;  //number of elements
    int i;
    int sequence[100];  //numeber if sequence
    int sum = 0.0;            //sum if the sequence
    float avg;          // the average

    printf("Enter the number of number in the sequence:\n");
    scanf("%d", &num);

    while (num < 0 || sequence[num] < 0) {
        printf("No positine numbers!\n");
    }

    printf("Enter the sequence:\n");
    for (i=0; i < num; i++) {
        scanf("%d", &sequence[i]);
        sum += sequence[i];
    }

    avg = (float) sum / num;
    printf("Average is %.2f\n", avg);
    return(0);
}

C Exercises: Counts the number of positive numbers and print the , C programming, exercises, solution: Write a C program that read 5 numbers and counts the number of positive numbers and print the average  C Program to find Sum and Average of n Number using For Loop. This C program allows the user to enter the number (n) he wishes to calculate the average and sum. Next, it will ask the user to enter individual items up to a declared number.

If you want the average of the positive numbers, you should use a different variable than num for your final avg calculation.

I would do something like this:

    int PositiveNumCount = 0;
    float avg;   
    for (i=0; i < num; i++) {
        scanf("%d", &sequence[i]);
        if(sequence[i] > 0){ sum += sequence[i]; PositiveNumCount++;}
    }
    avg = (float) sum / PositiveNumCount;

Find negative numbers from array of integers using C# program , How do you find a negative number in an array? C/C++ :: Calculate Sum And Average Of A Sequence Of Numbers Feb 17, 2014. Write a C program that calculates the sum and average of a sequence of numbers. Assume the first number specifies the number of values remaining to be entered. If the first number is less than 1, then the program should display an "Invalid Entry Please enter a

Here:

while (num < 0 || sequence[num] < 0) {

you are testing sequence[num] before you have written any values to the array, and the test of num should be num <= 0 since if there are zero values, there are also no positive values. You can only decide that there are no positive values after you have entered them all.

The array sequence is unnecessary - you can simply accumulate the positive values to sum and discard them, maintaining a count of valid values. The avoids any buffer overrun issues also.

sum is an int, but you have unnecessarily initialised it with a double.

Your average calculation:

avg = (float) sum / num ;

is incorrect since num includes negative values - you need a separate count of valid values contributing to the average (unless the intent is to treat all negative values as zero).

Another issue is that you have not validated the input matched the format specifier.

int main()
{
    printf("Enter the number of number in the sequence:\n");
    int num = 0 ;
    int check = scanf( "%d", &num ) ;

    int positive_count = 0 ;
    int sum = 0 ;
    if( check != 0 && num >= 0 )
    {
        printf( "Enter the sequence:\n" ) ;

        int i = 0 ;
        while( i < num ) 
        {
            int value = 0 ;
            check = scanf( "%d", &value ) ;
            if( check != 0 )
            {
                i++ ;
                if( value >= 0 )
                {
                    positive_count++ ;
                    sum += value ;
                }
            }
        }
    }

    if( positive_count != 0 )
    {
        float avg = (float) sum / positive_count ;

        printf( "Average is %.2f\n", avg ) ;
    }
    else
    {
        printf( "No positive numbers!\n" ) ;
    }

    return 0 ;
}

Average of even numbers till a given even number?, How do you find the average of 5 numbers in C? How To Write a C program To Find The Average of Three Numbers in This Video. some related videos. 4. Write A Program In C To Find a Number of Positive and Negative

printf("Enter the sequence:\n");
for (i=0; i < num; i++) {
    scanf("%d", &sequence[i]);
    if(sequeunce[i] > 0){ //This if statement is what your looking for, but there are several other problems in your code.  GL!
        sum += sequence[i];
    }
}

The above if statement is what the OP was asking for: "Here is my code, I just need help with how to ignore the negative numbers in the input sequence."

There are many issues with his posted code, including using variables before they are initialized and infinite looping paths. This is the shortest answer to his question.

How do I add negative numbers in C? : C_Programming, status (or a status above 255). signed int x = 1 -3 +5; ? You need to give us some code if you really want help. Enter number: 45.6 Average = 27.69. This program calculates the average if the number of data are from 1 to 100. If user enters value of n above 100 or below 100 then, while loop is executed which asks user to enter value of n until it is between 1 and 100.

Solved: Write A Program That Calculates The Average Of A S , The User Can Enter As Many Non-negative Numbers As They Want, And They For This Program, Zero Counts As A Number That Goes Into The Average. the user if they want to calculate another average for a different set of numbers. Fill in the method with a loop that runs while the user enters non-negative numbers. Number of ways to obtain each numbers in range [1, b+c] by adding any two numbers in range [a, b] and [b, c] Count of subarrays of size K which is a permutation of numbers from 1 to K; Count of triplets of numbers 1 to N such that middle element is always largest; Minimum LCM of all pairs in a given array

Move all negative numbers to beginning and positive to end with , Segregating negative and positive maintaining order and O(1) space · Find ratio of zeroes, positive numbers and negative numbers in the Array · C program to  I have to design a C++ program that reads a sequence of positive integer values that ends with zero and find the length of the longest increasing subsequence in the given sequence. For example, for the following sequence of integer numbers:

Average of even numbers till a given even number, We can calculate average by adding each even numbers till n and then dividing sum by count. C++. Please help out here. I want to create a program whereby a user inputs several numbers (let's say 6 numbers from his/ her head). The program should then go ahead and calculate the sum of all these numbers. I however have to use a loop statement, either For statement, While statement or do/while statement. This is what I have so far:

Comments
  • A hint for starters: You do not need to store the full sequence of numbers, just add up the positive ones and keep a counter tracking how many you added. Divide after that.
  • you never break the while loop once you enter it. "No positine numbers!\n" spammed forever. you dont initialize any of your variables when you declare them, so you use squence[num] before its initialized. but to answer your question, dont add sum += sequence[i] when sequence[i] is <= 0.
  • Please explain each line of your coding attempt, by adding a comment. It is very unclear what your thinking is. What is the idea of sequence[num] < 0? Where in your code is anything related to "only positive numbers"?
  • This will give a wrong average, since you still have to track how many positive numbers you have added
  • Would you like to improve this code-only answer by adding some explanation?
  • From the OPs only question: "Here is my code, I just need help with how to ignore the negative numbers in the input sequence." This code is riddled with issues...
  • @Yunnosch I have clarified my response, thank you for the suggestion.
  • Pointing out shortcomings and giving the shortest possible answer is not the same as a helpful answer and not the explanation to improve the answer either.