How do I show the number in case 3 ? For example, if I input 1992 , how to write
ID: 3715093 • Letter: H
Question
How do I show the number in case 3?
For example, if I input 1992, how to write a code to show that how many 9 numbers in the element?
It's supposed to be 2, but my code shows 4.
I know I cannot use "Index", but what supposed to use?
My code below:
---------------------------------------------------------------
#include <iostream>
#include <stdlib.h>
#include <conio.h>
#include <string>
using namespace std;
struct Node
{
Node *previous;
Node *next;
int number;
};
int main()
{
Node *head = NULL;
Node *tail = NULL;
Node *n;
int opt, index = 0, temp, number;
char ans = 'y';
while (ans == 'y')
{
system("cls");
cout << " Menu:"
<< " 1 - Insert a new data at the Begining of the list"
<< " 2 - Insert a new data at the End of the list"
<< " 3 - Find a Node via its data element"
<< " 4 - Traverse and Display list from Begining to End"
<< " 5 - Traverse and Display list in Reverse order"
<< " 6 - Change any of the fields based on its value"
<< " 7 - Quit"
<< " Enter an option from 1 - 7: ";
cin >> opt;
switch (opt)
{
case 1:
system("cls");
n = new Node;//command new create
index++;
cout << "Enter an integer value in the nodes: ";
cin >> temp;
n->previous = NULL;
n->number = temp;
n->next = head;
head = n;
if (index == 1)
{
tail = n;
}
if (index > 1)
{
n = n->next;
n->previous = head;
}
break;
case 2:
system("cls");
n = new Node;//comand new create
index++;
if (index == 1)
{
head = NULL;
}
cout << "Enter an integer value in the nodes: ";
cin >> temp;
n->previous = tail;
n->number = temp;
n->next = NULL;
tail = n;
if (head == NULL)
{
head = n;
n->previous = NULL;
}
else
{
n = n->previous;
n->next = tail;
}
break;
case 3:
system("cls");
cout << "Enter the number you want to search: ";
cin >> temp;
n = head;
while (temp != NULL)
{
if (n->number == temp)
{
cout << " Number " << n->number << " Found, and total are " << index << " values.";
getch();
break;
}
else
{
// NOT Found, move to the NEXT Node.
n = n->next;
if ((n->next == NULL) && (n->number != temp))
{
cout << " " << temp << " is NOT in the LIST!";
getch();
break;
}
}
}
break;
case 4:
// Traverse and Display List Elements
if( index > 0 )
{
cout << " List has " << index << " elements. ";
n = head;
while(n->next != NULL)
{
cout << n->number << " ";
n = n->next;
}
cout << n->number << " NULL - End of List "
<< " Hit Any Key to continue...";
}
else
cout << "List is empty!"
<< " Hit Any Key to continue...";
getch();
break;
case 5:
// Traverse and Display List Elements in REVERSE order
if( index > 0 )
{
cout << " List has " << index << " elements. ";
n = tail;
while(n->previous != NULL)
{
cout << n->number << " ";
n = n->previous;
}
cout << n->number << " NULL - End of List "
<< " Hit Any Key to continue...";
}
else
cout << "List is empty!"
<< " Hit Any Key to continue...";
getch();
break;
case 6:
system("cls");
cout << "Enter the number you want to change: ";
cin >> temp;
n = head;
while (temp != NULL)
{
if (n->number == temp)
{
cout << "Enter a new number: ";
cin >> n->number;
break;
}
else
{
n = n->next;
if ((n->next == NULL) && (n->number != temp))
{
cout << " " << temp << " is NOT in the LIST!";
getch();
break;
}
}
}
break;
case 7:
system("cls");
ans = false;
break;
default:
system("cls");
cout << opt << " is NOT a Valid value!";
cout << " Hit Any Key to continue...";
getch();
}
}
}
Explanation / Answer
This code resolves the below error with using index
int main()
{
int n, , remainder,a=9;
printf("Enter an integer: ");
scanf("%d", &n);
while(n != 0)
{
remainder = n%10;
if(remainder==a)
{
count++;
}
n /= 10;
}
printf("Reversed Number = %d", count);
return 0;
}
o/p: Enter the n value is 1993
count is 2
o/p :enter the n value 1999
count is 3
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.