Question

Write a program in C that meets the following requirements: 1. Your program will read n...

Write a program in C that meets the following requirements:
1. Your program will read n + 1 integers from the keyboard where n is the first integer read.

2. If n is less than 1 print an error message and return a status of 1.

3. If there are no positive numbers, print “no positive numbers”. This is not an error.

4. If there are positive numbers, print the average of the positive numbers.

5. 0 is neither positive or negative.

6. Do not print anything other than what is required.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Answer :

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

int main()
{

//Declare and initialize all the variables which are used in the code.
int n,i,a[1000],status,count=0,count1=0,count2=0;
float b,avg=0;

//Read the input size n
printf("Enter the value of n\n");
scanf("%d",&n);

//check whether it is less than 1.

if(n<1) {
   printf("Error found\n");
   status = 1;
   return status;
}

//Read n+1 integers.

printf("Enter n+1 Integers\n");
for(i=0;i<=n;i++) {
   scanf("%d",&a[i]);
}

//Check each integers as below

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

//if integer is less than 0 increment count
if(a[i]<0) {
count++;
}

// if integer is greater than 0 positive one then increment count1
else if(a[i]>0) {
count1++;
b=b+a[i];
}

// if integer is 0 then increment count2
else {
count2++;
}
}

//Check whether all integers are 0

if(count2==n+1){
printf("0 is neither positive nor negative\n");
}

//check whether all integers are negative
if(count ==n+1){
printf("no positive numbers\n");
}

// check if positive integer is present then find the average
if(count1 >0) {
avg = b/count1;
printf("Average of positive numbers = %f\n",avg);
}
}

Output :

Add a comment
Know the answer?
Add Answer to:
Write a program in C that meets the following requirements: 1. Your program will read n...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • C Programming Write a program that meets the following requirements: • Your program will read three...

    C Programming Write a program that meets the following requirements: • Your program will read three integer values from the keyboard. • Your program will output the following data in this order: 1. The third divided by the first if the first is not zero. 2. The third divided by the second if the first is zero and the second is not zero. 3. The sum of the first, second and third values. • All numbers are integers and only...

  • Write a C program that accepts a single integer N. Your code must then calculate the...

    Write a C program that accepts a single integer N. Your code must then calculate the average of all positive integers less than N that are divisible by either 5 or 7, but not both. When you print your average, truncate the result to three decimal places. Do not print anything else to the screen. Example input: 19 Example output: 10.200

  • C++ Write a program that prompts the user to enter two positive integers: num1 and num2....

    C++ Write a program that prompts the user to enter two positive integers: num1 and num2. - Validate that num1 is less than num2 and that both numbers are positive. If any of these conditions are not met, allow the user to re-enter num1 and num2 until the input is determined valid. - For all integers from num1 through num2, print the word keyboard if the current integer is divisible by 2 and print the word mouse if the current...

  • C++ Write a program that prompts the user to enter integers or a sentinel to stop....

    C++ Write a program that prompts the user to enter integers or a sentinel to stop. The program prints the highest and lowest numbers entered, the sum and the average. DO NOT USE ARRAYS, only variables and loops. Write a program the prompts the user to input a positive integer. Keep asking the user until a positive integer is entered. Once a positive integer is entered, print a message if the integer is prime or not. (NOTE: A number is...

  • using C++ Write a program that: a) Inputs an integer n from the keyboard where n<=100....

    using C++ Write a program that: a) Inputs an integer n from the keyboard where n<=100. If n is out of range then print out an error message and ask for another input. This process repeats until a valid value for n is obtained. b) Inputs two 1D arrays of doubles A and B (of size n) from the keyboard. c) Inputs an integer k (from 1 to 3) from the keyboard. d) If k = 1 then it calculates...

  • c++ please (1) Write a program that prompts the user to enter an integer value N...

    c++ please (1) Write a program that prompts the user to enter an integer value N which will rpresent the parameter to be sent to a function. Use the format below. Then write a function named CubicRoot The function will receive the parameter N and return'its cubic value. In the main program print the message: (30 Points) Enter an integer N: [. ..1 The cubic root of I.. ] is 2 update the code om part and write another function...

  • P.2. Division Write a C program to input an integer n. Then the program asks users...

    P.2. Division Write a C program to input an integer n. Then the program asks users to input n integers. Calculate the sum of all non negative integers and the division of this with every negative integer in the sequence. p3 Write a C program to print all 3-digit abc numbers, satisfying: (a+b+c)=(a.b.c). Example: 3-digit 123 number fulfills requirement as (1+2+3)=1.2.3

  • Q1. Write a C++ program that reads two integers a, and, b on the keyboard to...

    Q1. Write a C++ program that reads two integers a, and, b on the keyboard to calculate: U = (a + b)2 and V = (a - b)2 Q2. Write a C++ program to read a sequence of positive integers and to display the largest integer of that sequence. Use a negative integer to indicate the end of the data entry.

  • Ques) Write a program in c, which meets the following requirements. Requirements 1. Read integer values...

    Ques) Write a program in c, which meets the following requirements. Requirements 1. Read integer values from stdin, separated by one or more spaces or newlines, until reaching EOF 2. The input is guaranteed to be well-formed. 3. The input contains no more than 80 values. 4. on standard output, render a simple vertical column graph representation of the input values, in order left to right, using hash'#' characters as shown in the examples below. The number of hashes printed...

  • in C The preceding LML program reads two numbers from the keyboard and determines and prints the larger value. Note the use of the instruction +4107 as a conditional trans- fer of control, much...

    in C The preceding LML program reads two numbers from the keyboard and determines and prints the larger value. Note the use of the instruction +4107 as a conditional trans- fer of control, much the same as C's if statement. In-Class Tasks Write LML programs that accomplish each of the following tasks: Use a sentinel-controlled loop to read 10 positive integers and compute and print their sum 1. 2. Use a counter-controlled loop to read seven numbers, some positive and...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT