Question

In: Electrical Engineering

A matlab program to enter N numbers and count the positive, negative and zero numbers. Then...

A matlab program to enter N numbers and count the positive, negative and zero numbers. Then print the results

Solutions

Expert Solution

The matlab program to count positve negative and zeros of the enterd no :

N=input("enter a no of elements") % asking no of elements
  
arr=zeros(1,N); %intitialzing empty arry with 1 row and n no of columns
for i=1:1:N % for loop to get the elemnts from user
k=input("enter the elements"); % asking every element in the arry
arr(i)=k; % assigning value to arry
end % ending the for loop

fprintf(" the arry is :\n"); % printing the arry
for i=1:1:N % for loop to print the elements of the arry
disp(arr(i)) % displaying arry elements
end % ending the for loop

pos=0;neg=0;zero=0; % intitalizing the positive,negative,zeros are 0

for i=1:1:N % for loop to access elements in the arry
if(arr(i)==0) % checking if the elment is 0 or not
  
zero=zero+1; % incrementing zeros if condition is true
  
elseif(arr(i)<0) % checking the element is negative or not
  
neg=neg+1; % incrementing neg if condition is true
else
pos=pos+1; % if element is not zero and negative then increments the pos
end % ending the condition
end % ending the for loop

fprintf("the total no of positive no are : %d \n",pos); % prints total no of pos no
fprintf("the total no of negative no are : %d \n",neg); % prints total no of neg no
fprintf("the total no of zeros no are : %d \n",zero); % prints total no of zeros

The ouput for the above program is :-

  


Related Solutions

For example, suppose you had a program to determine if a number is positive, negative or zero.
  For example, suppose you had a program to determine if a number is positive, negative or zero. Read in a number and then use the if statement to determine if the number is positive (>0) or negative (<0) or zero (==0): if (x>0)//display positive messageelse if (x<0)   //display negative messageelse//display zero message
USE PYTHON. Create a program that will ask a user to enter whole positive numbers. The...
USE PYTHON. Create a program that will ask a user to enter whole positive numbers. The program will use a while loop to let the program keep running. The user will be allowed to use the program as long as the quitVariable is equal to 'y' or 'Y'. When the user decides to quit playing, say "Thanks for playing!". The program will use a for loop to test if the number are even or odd. If even print "number is...
Write C program : Enter N and N pairs of positive integerpairs, convert each pair...
Write C program : Enter N and N pairs of positive integer pairs, convert each pair of positive integers into binary and add, and output the formula. Decimal to binary function prototype: int DecToBin(int integer);The range of input N is 1 <= N <= 100, and the range of positive integers input is 0 < n <= 255. The output format is %08d. If the entered N or positive integer is out of range, "Invalid input" is output. If a...
                         Positive       Negative Influenza A.       &n
                         Positive       Negative Influenza A.         360.              7 no influenza A.      6.               1110 find the probability of a subject with a positive test result given that the subject does have influenza A find the probability of selecting a subject with a negative test result given that the subject does not have influenza a
A. Write a program 1. Prompt the user to enter a positive integer n and read...
A. Write a program 1. Prompt the user to enter a positive integer n and read in the input. 2. Print out n of Z shape of size n X n side by side which made up of *. B. Write a C++ program that 1. Prompt user to enter an odd integer and read in the value to n. 2. Terminate the program if n is not odd. 3. Print out a cross shape of size n X n...
Java Program Problem // Description: Read five positive integers and average them. // Enter a negative...
Java Program Problem // Description: Read five positive integers and average them. // Enter a negative integer to end the program. import java.util.Scanner; public class Lab5 { public static void main (String args[]) {        Scanner sc;        sc = new Scanner(System.in);    final int MAX_NUMS = 5;        int num;       // variable to store the number        int sum;   // variable to store the sum        int count;    // variable to store...
JAVA Language: Write a program that prompts the user to enter a positive integer n (0...
JAVA Language: Write a program that prompts the user to enter a positive integer n (0 up to 232 -1). You must write a function that takes as input n and returns a string s representing the number n in binary. For this assignment, you must use the method of successive division by 2 to convert the number to binary. Your main program must print out s. Example: If the user enters the number 66, your program must print out...
Write a c++ program of the Fibonacci Sequence. Have the user enter a positive integer n...
Write a c++ program of the Fibonacci Sequence. Have the user enter a positive integer n and compute the nth Fibonacci number. The program should end when the user enters a number less than or equal to zero
Input 100 numbers and find and output how many numbers are positive and negative, find and...
Input 100 numbers and find and output how many numbers are positive and negative, find and output average of these numbers Write using vb.net
Write a C program that prompt the user to enter 10 numbers andstores the numbers...
Write a C program that prompt the user to enter 10 numbers and stores the numbers in an array. Write a function, smallestIndex, that takes as parameters an int array and its size and return the index of the first occurrence of the smallest element in the array.The main function should print the smallest number and the index of the smallest number.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT