Write a program with a function main () and selecting a menu of functions:
Generate a programming-random number generator data for lottery ticket / max 100 / with six-digit numbers and store them in an array
-Overwrite generated a new array and sort this array in ascending order and display output
-Counting and display output and numbers of all "happy" six digit lottery tickets /these numbers which sum of the first 3 digits is equal to the last three/
-Save in the array and display output sequence numbers of downloaded "lucky" lottery tickets
I have a problem with last two steps.Can you explain how to do it correctly?
#include <cstdlib>
#include <ctime>
#include <cstring>
#include <iostream>
using namespace std;
int numbers;
int array[100];
void generator()
{
srand((unsigned)time(0));
for(int i = 0; i < 100; i++)
{
numbers = (rand() % 900000) + 100000;
array[i] = numbers;
cout << numbers << endl;
}
}
void sorted_list()
{
int i = 100, a, b, c;
for (a = 0; a < i - 1; a++)
{
for(b = 1; b < i; b++)
{
if (array[b] < array[b - 1])
{
c = array[b];
array[b]= array[b - 1];
array[b - 1]=c;
}
}
}
for (a = 0; a < i; a++)
{
cout << a << ": " << array[a] << "\n";
}
}
bool happyNr(int a){
if (a<100000 || a>999999) return false;
int half1=0, half2=0;
for (int i=0;i<6;i++){
if (i<3) {half1+=a%10; a=a/10;}
else {half2+=a%10; a=a/10;}
}
return half1==half2;
}
int menu()
{
int choice;
cout<<"\n_______________MENU_______________";
cout<<"\n 1. Generate random numbers";
cout<<"\n 2. Sorted array";
cout<<"\n 3. Happy numbers";
cout<<"\n 4. Exit";
do
{
cout<<"\n Choice: ";
cin>>choice;
}while(choice<1||choice>4);
return(choice);
}
int main()
{
int i;
do
{
i=menu();
switch(i)
{
case 1: generator();break;
case 2: sorted_list();break;
case 3: happyNr(i);break;
}
}
while(i!=4);
return 0;
}
Related
Write a C++ program that reads from the keyboard up to 10 numbers, using a while loop with a SENTINEL, to count and add the positive values and then display the results. Your program should terminate whenever it either reads 10 number or a negative number. You assume at least there is one number.
This is what I've tried, but I couldn't figure out a way to basically keep reading the inputs without submitting Enter until it finds a negative number or reaches the input limit (10) so it stops.
#include <iostream>
using namespace std;
int main()
{
const int SENTINEL = 10;
int number;
int count = 0;
int sum = 0;
cout << "enter 10 random numbers" << endl;
while (count != SENTINEL)
{
cin >> number;
sum = sum + number;
count++;
}
return 0;
}
What about:
#include <iostream>
using namespace std;
int main()
{
const int SENTINEL = 10;
int number;
int count = 0;
int sum = 0;
int stop = 0;
int aux = 0;
cout << "enter 10 random numbers" << endl;
while (count != SENTINEL && stop == 0)
{
cin >> number;
if(number > 0){
sum = sum + number;
aux++;
} else {
stop = 1;
}
count++;
}
printf("Number of positives number is %d \n", aux);
printf("The sum of the positives numbers is %d", sum);
return 0;
}
Solution your problem:
#include <iostream>
using namespace std;
int main()
{
const int SENTINEL = 10;
int number;
int count = 0;
int sum = 0;
cout << "enter 10 random numbers" << endl;
while (cin >> number&&count != SENTINEL&&number>0)
{
sum = sum + number;
count++;
}
cout<<sum<<endl;
return 0;
}
Alright, so I have this code which basically will prompt the user to enter a number and then according to that number the user entered, they will be asked (Please enter a number between 1 and 4)* the number the user chose. Then, their input will be compared to see if there is any match in the grid (rows and columns). Let me show you an example:
Please enter a number: 3
Please enter a number between 1 and 4: 2
Please enter a number between 1 and 4: 1
Please enter a number between 1 and 4: 2
Here is your grid:
(3x3 grid filled, since user entered 3, filled with random numbers)
4 2 4
3 1 1
4 3 3
Here is a sample of my code:
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <iomanip>
#include <time.h>
#include <ctime>
using namespace std;
double Atemp = 0;
double Utemp = 0;
double Working = 0;
double Total = 0;
char Answer = 'x';
int Umain;
void printGrid(int &Umain);
void fillIntArray(int array[], int size);
void reverseArray(int array[], int size);
void outputIntArray(int array[], int n);
void compareGrid(int &Atemp);
int main(){
int maxNum = 2;
int intArray[maxNum];
cout << "Please Enter numbers between 1 and 12: ";
cin >> Umain;
do{
if(Umain <=12){
fillIntArray(intArray, maxNum);
//outputIntArray(intArray, maxNum);
printGrid(Umain);
}
}while (Answer == 'y');
return 0;
}
void fillIntArray(int array[], int size){
do{
for (Utemp = Umain; Utemp > 0; Utemp--){
cout << "Please enter a number between 1 and 4: ";
cin >> Atemp;
if(Atemp <=4 && Atemp >=1){
for (int i = Atemp; i < Atemp; i++);
}else{
cout << "Not within limit \n";
}
}
}while (Answer == 'y');
}
void printGrid(int &Umain){
cout<<endl;
cout<<" ";
int i=1,j;
for(j = 0; j <= 4*Umain; j++){
if(j%4==2){
cout<<" ";
}
}
cout<<endl;
for(i = 0; i <= 2*Umain; i++){
for(j = 0; j <= 2*Umain; j++){
if(i%2==0){
if(j==0){
cout<<" ";
}
if(j%2==0){
cout<<" ";
}else{
cout<<"---";
}
}else{
if(j%2==0){
cout<<" | ";
}else cout<< (rand()%4+1);
}
}
if(i%2!=0){
cout<<" ";
}
cout<<endl;
}
cout<<" ";
for(j = 0, i = 1; j <= 4*Umain; j++){
if(j%4==2){
cout<< " ";
}
}
cout<<endl;
}
void compareGrid(int &Atemp){
}
For one thing, variable size arrays are not a part of C++. This part is wrong:
int maxNum = 2;
int intArray[maxNum];
Fix it by adding constexpr:
constexpr int maxNum = 2;
int intArray[maxNum];
Secondly, this part in general doesn't make sense and the semicolon at the end is really suspicions:
for (int i = Atemp; i < Atemp; i++); // why?
And assuming you have collected the user inputs in an array called Attempts of size 3, and intArray is the matrix of size 3x3, here's how you might compare attempts to the rows/columns of the so-called 2D array:
bool Contains() {
for (size_t iCol = 0; iCol != 3; ++iCol) {
for (size_t iRow = 0; iRow != 3; ++iRow) {
if(intArray[iCol][iRow] != Attempts[iRow]) {
return false;
}
}
}
return true;
}
Change places of iCol and iRow to check rows or columns
I've got a menu that I've been working on, and everything now seems to work, except for the binary search function. I am entering the size of the array, filling the array, printing it, then sorting it with my chronological menu, and then when I do sequential search, it seems to work. However, binary search does not even return -1 to indicate that the number was not found, the program just stops. Suggestions? Thanks.
#include<iostream>
#include <cmath>
#include <cstdlib>
using namespace std;
int size=0; //global variable size
int a[100]; //global array
int getSize()
{
cout << "Array Size: ";
cin >> size;
return size;
cout << endl;
}
int sequentialSearch(int n)
{
for(int i=0;i<size;i++)
{
if(n==a[i])
{
return (i+1);
}
}
return -1;
}
int binarySearch(int n)
{
int low=0,high=size,mid;
while(low<=high)
{
mid=(low+high)/2;
if(a[mid]==n)
{
return mid+1;
}
else if(a[mid]>n)
high=mid-1;
else if(a[mid]<n)
low=mid-1;
}
return -1;
}
void sort1()
{
int i, j;
for (i = 0; i < size-1; i++)
{
for (j = 0; j < size-i-1; j++)
if (a[j] > a[j+1])
{
int temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
void sort2()
{
int i, j, m;
for (i = 0; i < size-1; i++)
{
m = i;
for (j = i+1; j < size; j++)
{
if (a[j] > a[m])
m = j;
}
int temp=a[m];
a[m]=a[i];
a[i]=temp;
}
}
void printMenu()
{
cout<<"0. Exit\n";
cout<<"1.Get the size needed for todays use of the array \n";
cout<<"2.Fill an array with random numbers from 1-100 \n";
cout<<"3.Print the array with position numbers \n";
cout<<"4.Sort the array in ascending sequence \n";
cout<<"5.Sort the array in descending sequence \n";
cout<<"6.Sequential search of the array for a target\n";
cout<<"7.Binary search of the array for a target\n";
}
void printTheArray()
{
for(int i=0;i<size;i++)
{
cout<<a[i]<<" is at position :"<<i+1<<endl;
}
}
void fillWithRandom()
{
for(int i=0;i<size;i++)
{
int x=rand()%101;
a[i]=x;
}
}
void dispatch(int ch)
{
switch(ch)
{
case 1:cout<<"Size of array :"<<getSize() << endl;
break;
case 2:fillWithRandom();
break;
case 3: printTheArray();
break;
case 4:sort1();
break;
case 5:sort2();
break;
case 6:
{
cout<<"Enter the number you want to search\n";
int t;
cin>>t;
int res1=sequentialSearch(t);
if(res1!=-1)
cout<<"Found in position :"<<res1<<endl;
else if(res1==-1)
cout<<"Not Found \n";
break;
}
case 7:
{
cout<<"Enter the number you want to search\n";
int t;
cin>>t;
int res=binarySearch(t);
if(res!=-1)
cout<<"Found in position :"<<res<<endl;
else if(res==-1)
cout<<"Not Found \n";
break;
}
default:cout<<"wrong choice\n";
}
}
int main ()
{
printMenu();
int choice;
cout<<"Type in a choice"<<endl;
cin>>choice;
while(choice!=0)
{
dispatch(choice); // one big switch statement
printMenu();
cout<<"Type in a choice"<<endl;
cin>>choice;
}
cout << endl;
// wrap-up
cout << "This program is coded by Troy Wilms" << endl; // fill in your name
// stops the program to view the output until you type any character
return 0;
}
for example, if you try finding 4 in list {1, 3, 5}, low will be always 0 and high will be always 2. because of the code 'low = mid - 1' I think it should be changed to 'low = mid + 1'
I wrote a simple C++ program that finds how many duplicates are in the array.
This works perfectly for me but this is very long code. And I would like to know if there is any short code which may perform this task successfully:
#include<iostream>
using namespace std;
int main()
{
int a[10];
int reper=0,word=0,flage=0,number[10]={
0
};
//Getting Input From User
for (int i = 0; i <=9; i++)
{
cout<<"Enter The Value For "<<i<<" Index"<<endl;
cin>>a[i];
}
//Checking The Duplicates Numbers
for (int i = 0; i <= 9; i++)
{
reper=0;
flage=0;
for (int j = 0; j <=9; j++)
{
if (a[i]==a[j])
{
if (i!=j)
{
reper++;
}
}
}
number[i]=a[i];
for (int k = 0; k <=9; k++)
{
if (i!=k)
{
if(number[i]==number[k])
{
flage=1;
break;
}
}
}
//If There Are Duplicates Then Prints That Numebr, How Many Times It Repeated And Total Occurance Of That Number In The Array
if (reper!=0&&flage==0)
{
cout<<"Repeated Number Of The Array Is : "<<a[i]<<" ";
cout<<"And This Number Repeated "<<reper<<" Times "<<"And Total Occurance Of This Number is : "<<reper+1<<endl;
word=a[i];
}
}
//If There Is Nothing Any Duplicate In The Array Then Simply Prints This Message On Console
if (reper==0&&word==0)
{
cout<<"There Is Nothing Any Repeated Number Of This Array: "<<endl;
}
system("Pause");
return 0;
}
IMHO the easiest way to implement this - using http://en.cppreference.com/w/cpp/container/multiset. It has logarithmic complexity and inner methods to count repeated items.
Refer an example below:
#include <iostream>
#include <set>
int main(int argc, char *argv[])
{
std::multiset<int> ms;
//Getting Input From User
for (int i = 0; i <=9; i++)
{
std::cout<<"Enter The Value For "<<i<<" Index"<<std::endl;
int val;
std::cin>>val;
ms.insert(val);
}
bool repeated_number_found=false;
std::multiset<int>::const_iterator it = ms.begin();
while (it != ms.end()) {
int reper=ms.count(*it);
if (reper > 1){
std::cout << "Number " << *it << " repeated for " << reper << " times" << std::endl;
repeated_number_found=true;
}
it = ms.upper_bound(*it);
}
if (!repeated_number_found){
std::cout<<"There Is Nothing Any Repeated Number Of This Array"<<std::endl;
}
return 0;
}
But using this container you will loose first entrance of repeated number, if it matters to you, I will recommend using struct or std::pair to hold entrance number with entered number. In this case you will need to provide custom comparator also (refer to doc.)
I think the better way to achieve this would be to sort the array and do something like this :-
(include the header file algorithm before doing this.)
vector <int> a (10,0);
for (int i = 0; i <=9; i++)
{
cout<<"Enter The Value For "<<i<<" Index"<<endl;
cin>>a[i];
}
int count = 0;
sort(a.begin(), a.end());
for(int i = 0; i < a.size() - 1; i++) {
if (a[i] == a[i + 1]) {
count++;
}
}
cout << count << endl;
Ok so I've already simplified/condense it while keeping the functionality,but im only doing C++ for a month and a half.Was at 100 lines of code.Is it possible to declare the variable in the functions arguement and then call them without passing values into the arguemnets?
#include <iostream>
#include <windows.h>
using namespace std;
int primeCheck10 (int j)
{
int count=0;
cout<<"Enter a number between 1 and 10___";
cin>>j;
if(j<1 ||j>10)
{
cout<<"Invalid Value\n";
return 0;
}
for(int i=2; i<j; i++)
{
if(j%i==0)
{
count++;
break;
}
}
if(count==0)
cout<<"Prime number\n";
else
cout<<"Not a Prime number\n";
}
int primeCheck100(int j)
{
int count=0;
cout<<"Enter a number between 1 and 100___";
cin>>j;
if(j<1 || j>100){
cout<<"Invalid Value\n";
return 0;
}
for(int i=2; i<j; i++)
{
if(j%i==0)
{
count++;
break;
}
}
if(count==0)
cout<<"Prime number\n";
else
cout<<"Not a Prime number\n";
}
int checkPrime1000(int j)
{
int count=0;
cout<<"Enter a number between 1 and 1000___";
cin>>j;
if(j<1 || j>1000){
cout<<"Invalid Value\n";
return 0;
}
for(int i=2; i<j; i++)
{
if(j%i==0)
{
count++;
break;
}
}
if(count==0)
cout<<"Prime number\n";
else
cout<<"Not a Prime number\n";
}
int main ()
{
system("pause");
return 0;
}
Yes, you can condense all the prime checking into a single function pretty easily. I'd change the structure of the code to just check whether a given number is prime, and return a bool to indicate whether it is or not:'
bool isprime(int n) {
int limit = sqrt(n)+1; // only need to check up to sqrt(n)
if (n == 2)
return true;
if (n == 1 || n % 2 == 0) // check if it's 1 or even
return false;
for (int i = 3; i <= limit; i += 2) // not even -- only check odd numbers
if (n % i == 0)
return false;
return true;
}
Then the code to get input and display results would be separate:
void primecheck(int limit) {
std::cout << "Please enter a number between 1 and " << limit;
int j;
std::cin >> j;
if (j<1 || j > limit)
std::cerr << "Invalid value";
static char const *labels [] = { "Not a prime number\n", "Prime number\n" };
std::cout << labels[isprime(j)];
}
It wouldn't be terribly difficult to make it even shorter than this, but we're reaching the point where it would probably end up less readable if you did so.
If I have unserstood correctly you then you are asking about default arguments. For example
#include <iostream>
void f( int i = 10 )
{
std::cout << "i = " << i << std::endl;
}
int main()
{
f();
}
You could just make one method... and add a bounds
int primecheck(int value, int bounds){
int count=0;
cout <<"Enter a number between 1 and " << bounds << "___";
cin>>value;
if(j<1 || value>bounds){
cout<<"Invalid Value\n";
return 0;
}
for(int i=2; i<value/2; i++)
{
if(value%i==0)
{
count++;
}
}
if(count==0)
cout<<"Prime number\n";
else
cout<<"Not a Prime number\n";
return count;
}
This alg is prob not the best way to check for primes though. for instance for(int i=2; i<j/2; i++) in your loop with optimize it. For example 32, 6x6 = 32, 2x16 = 32, 4x8 = 32. The highest number that can be divisible to j or value is half of it because 1 isn't considered under divisibility so the lowest number would be 2. Which is half.