In JAVA Please!! Write a program that reads in up to 1000 non-negative integers
ID: 3557649 • Letter: I
Question
In JAVA Please!!
Write a program that reads in up to 1000 non-negative integers and displays distinct numbers (i.e., if a number appears multiple times, it is displayed only once). (Hint: Read each number and store it in an array if it is new. If the number is already in the array, ignore it.) The user will indicate that they are done entering numbers by entering a negative number. After the input, the array must contain the distinct numbers. Here is a sample run:
To get credit for this assignment you must use appropriate decomposition! You should have a method to read the numbers and a method to print the resulting array. The method to read the numbers should call an additional method that reads (and processes) a single number.
Explanation / Answer
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#define size 5
struct stack {
int s[size];
int top;
} st;
int stfull() {
if (st.top >= size - 1)
return 1;
else
return 0;
}
void push(int item) {
st.top++;
st.s[st.top] = item;
}
int stempty() {
if (st.top == -1)
return 1;
else
return 0;
}
int pop() {
int item;
item = st.s[st.top];
st.top--;
return (item);
}
void display() {
int i;
if (stempty())
printf(" Stack Is Empty!");
else {
for (i = st.top; i >= 0; i--)
printf(" %d", st.s[i]);
}
}
int main() {
int item, choice;
char ans;
st.top = -1;
printf(" Implementation Of Stack");
do {
printf(" Main Menu");
printf(" 1.Push 2.Pop 3.Display 4.exit");
printf(" Enter Your Choice");
scanf("%d", &choice);
switch (choice) {
case 1:
printf(" Enter The item to be pushed");
scanf("%d", &item);
if (stfull())
printf(" Stack is Full!");
else
push(item);
break;
case 2:
if (stempty())
printf(" Empty stack!Underflow !!");
else {
item = pop();
printf(" The popped element is %d", item);
}
break;
case 3:
display();
break;
case 4:
exit(0);
}
printf(" Do You want To Continue?");
ans = getche();
} while (ans == 'Y' || ans == 'y');
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.