program to find The number of prime numbers in array using cpp - c++

this is the problem
enter link description here
the problem in function prime but it think it is true , and i can not find solution
i submit it in codeforces but it give me .Wrong answer on test 5
:-
the input :
39
81 46 4 5 2 71 66 97 51 84 50 64 68 99 58 45 64 86 14 44 7 49 45 72 94 19 33 68 83 12 89 88 39 36 51 11 57 9 54
the wrong !!
#include <iostream>
#include <math.h>
using namespace std;
int maximum(int arr[], int n)
{
int max = INT_MIN;
for (int i = 0; i < n; i++)
{
if (max < arr[i]) { max = arr[i]; }
}
return max;
}
int minimum(int arr[], int n)
{
int min = INT_MAX;
for (int i = 0; i < n; i++)
{
if (min > arr[i]) { min = arr[i]; }
}
return min;
}
int prime(int arr[], int n)
{
int con = 0;
bool flag = true;
for (int i = 0; i < n; i++)
{
if (arr[i] == 2)
{
con++;
}
else if (arr[i] > 2)
{
for (int j = 2; j < n; j++)
{
if (arr[i] % j == 0)
{
flag = false;
break;
}
else
{
flag = true;
}
}
if (flag == true)
con++;
}
}
return con;
}
int palindrome(int arr[], int n)
{
int i = 0, con = 0;
while (n--)
{
int temp;
temp = arr[i];
int reverseNumber = 0, rightDigit;
while (temp != 0)
{
rightDigit = temp % 10;
reverseNumber = (reverseNumber * 10) + rightDigit;
temp = temp / 10;
}
if (reverseNumber == arr[i]) {
con++;
}
i++;
}
return con;
}
int divisors(int arr[], int n)
{
int max = 0;
int con = 0;
int x = arr[0];
for (int i = 0; i < n; i++)
{
int temp = arr[i];
for (int j = 1; j <= arr[i]; j++)
{
if (arr[i] % j == 0)
{
con++;
}
}
if (max < con)
{
max = con;
x = arr[i];
}
else if (max == con)
{
if (x < arr[i])
{
x = arr[i];
}
}
con = 0;
}
return x;
}
int main()
{
int n; cin >> n;
int arr[1001];
for (int i = 0; i < n; i++)
cin >> arr[i];
cout << "The maximum number : " << maximum(arr, n) << endl;
cout << "The minimum number : " << minimum(arr, n) << endl;
cout << "The number of prime numbers : " << prime(arr, n) << endl;
cout << "The number of palindrome numbers : " << palindrome(arr, n) << endl;
cout << "The number that has the maximum number of divisors : " << divisors(arr, n) << endl;
divisors(arr, n);
return 0;
}

This for loop
for (int j = 2; j < n; j++)
is incorrect. It seems you mean
for (int j = 2; j < arr[i]; j++)
Also you should declare the variable flag within the else statement where it is used. For example
for (int i = 0; i < n; i++)
{
if (arr[i] == 2)
{
con++;
}
else if (arr[i] > 2)
{
bool flag = false;
//...

Related

How to display this pyramid of numbers?

I have this task:
A user inputs a number N and you have to output this pyramid:
0
101
21012
.......
N.21012.N
For N=5 it will be :
0
101
21012
3210123
432101234
54321012345
I managed to only get it working for N<10 with this code:
int n;
cin >> n;
for (int i = 0; i < n + 1; i++) {
for (int j = 0; j < n - i; j++)
cout << " ";
int dir = -1;
for (int k = i; k <= i; k += dir) {
cout << k;
if (k == 0)
dir = 1;
}
cout << endl;
}
For N=10 it will look like this :
0
101
21012
3210123
432101234
54321012345
6543210123456
765432101234567
87654321012345678
9876543210123456789
10987654321012345678910
After the answers I settled on this :
#include <iostream>
#include <cmath>
#include <string>
using namespace std;
int main()
{
int n, spaces;
string number;
cin >> n;
if (n < 10)
spaces = n;
else
{
spaces = 9;
int pwr = 0, k = n;
while (k > 9)
{
pwr++;
k /= 10;
}
for (int i = 1; i < pwr; i++)
{
spaces += pow(10, i) * 9 * (i + 1);
}
spaces += (n - pow(10, pwr) + 1) * (pwr + 1);
}
// cout << spaces << endl;
for (int i = 0; i < n + 1; i++)
{
for (int j = i; j > -1; j--)
number += to_string(j);
int len = number.length() - 1;
for (int j = 0; j < spaces - len; j++)
cout << " ";
for (int j = 1; j <= i; j++)
number += to_string(j);
cout << number << endl;
number.clear();
}
cout << endl;
return 0;
}
int padding(int n) {
constexpr auto singleDigitNumbersCount = 9;
constexpr auto doubleDigitNumbersCount = 90; // from 10 to 99
if (n < 10) return n;
if (n < 100) return 2*n - singleDigitNumbersCount;
return 3*n - doubleDigitNumbersCount - 2*singleDigitNumbersCount;
}
int main() {
int n;
cin >> n;
for (int i = 0; i < n + 1; i++) {
std::cout << std::string(padding(n) - padding(i), ' ');
for (int k = i; k >= 0; k--) {
cout << k;
}
for (int k = 1; k <= i; k++) {
cout << k;
}
cout << '\n';
}
return 0;
}
https://godbolt.org/z/EEaeWEvf4
I made this a bit ago Compiler Explorer
Not sure if that'd help 🤔
Here is the working code:
#include <string>
#include <iostream>
using namespace std;
#define MAX_SPACE 50
int main()
{
int n;
cin >> n;
string output = "";
for (int i = 0; i < n + 1; i++)
{
for (int k = i; k >= 0; k--) {
output += to_string(k);
}
for (int k = 1; k <= i; k++) {
output += to_string(k);
}
for (uint8_t i = 0, max = MAX_SPACE - output.length() / 2.00; i < max; i++) // Print max spaces minus the integer length divided by 2
{
cout << " ";
}
cout << output << endl; // Print number
output = "";
}
return 0;
}

How to compare elements in an array for equality?

I've come across this problem where I have to transform a number from the decimal system to the binary and compare if the numbers are equal
Example:
7 is becoming 111
The output is false
and where 5 is 101
The output is true
I've figured out how to transform the numbers with an array
But have no idea how to compare them
#include<iostream>
using namespace std;
int main()
{
int num;
cin >> num;
int arr[8] = {};
if ((num >= 0) && (num <= 255))
{
while (num != 0)
{
for (int i = 0; i < 8; i++)
{
if (num % 2 == 0)
arr[i] = 0;
else
{
arr[i] = 1;
}
num = num / 2;
}
}
for (int i = 7; i >= 0; i--)
cout << arr[i];
cout << endl;
}
else
{
cout << "error" << endl;
return 1;
}
system("pause");
return 0;
}

Convert numbers into letters and multiply the number by itself

Sample Input #2
8
5
12
12
15
23
15
18
12
4
Sample Output #2
helloworld
8:8-16-24-32-40-48-56-64
5:5-10-15-20-25
12:12-24-36-48-60-72-84-96-108-120-132-144
12:12-24-36-48-60-72-84-96-108-120-132-144
15:15-30-45-60-75-90-105-120-135-150-165-180-195-210-225
23:23-46-69-92-115-138-161-184-207-230-253-276-299-322-345-368-391-414-437-460-483-506-529
15:15-30-45-60-75-90-105-120-135-150-165-180-195-210-225
18:18-36-54-72-90-108-126-144-162-180-198-216-234-252-270-288-306-324
12:12-24-36-48-60-72-84-96-108-120-132-144
4:4-8-12-16
#include <iostream>
#include <iomanip>
using namespace std;
char secretCode(char number)
{
if(number >= 1 && number <= 26)
{
return static_cast<char>('a' - 1 + number);
}
else if (number >= 27 && number <= 52)
{
return static_cast<char>('a' - 27 + number);
}
else if (number >= 53 && number <= 104)
{
return static_cast<char>('a' - 53 + number);
}
}
void printSequence(int number[10])
{
for (int i = 0; i < 10; i++)
{
cout << secretCode(number[i]);
}
}
int main()
{
int number[10];
for (int x = 0; x < 10; ++x)
{
cin >> number[x];
printSequence(number);
for (int y = number[x]; y <= number[x]; ++y)
{
for (int z = 1; z <= number[x]; ++z)
{
if (z > 1)
{
cout << "-";
}
if (z < 1)
{
cout << number[x] * -1;
}
else
cout << number[x] * z ;
}
}
}
}
I think I got the answer but I'm doing something wrong in my code, I'm pretty new to programming still and have been doing fine so far till I encounter the loop and functions..
#include<iostream>
#include<iomanip>
#include <iterator>
using namespace std;
char secretCode(char number)
{
if(number >= 1 && number <= 26)
{
return static_cast<char>('a' - 1 + number);
}
else if (number >= 27 && number <= 52)
{
return static_cast<char>('a' - 27 + number);
}
else if (number >= 53 && number <= 104)
{
return static_cast<char>('a' - 53 + number);
}
}
void printSequence(int number[10])
{
for (int i=0; i<10; i++)
{
cout << secretCode(number[i]);
}
cout << endl;
int len = 0;
while(len < 10){
cout << number[len] << ":";
for(int z=1; z<=number[len]; ++z){
if (z>1)
{
cout << "-";
}
if (z<1)
{
cout <<number[len];
}
else
cout << number[len]*z ;
}
cout << endl;
len += 1;
}
}
int main()
{
int number[10];
for(int x=0; x<10; ++x)
{
cin >> number[x];
}
printSequence(number);
}

C++ inserting array values into another array, but combining similar values into the same elements

So far I have gotten most of my functions working. I am now stuck on my final step. I need to take in an array from my main, to my "histo" function. I need to take repeating values within certain ranges, and put them each into the same array element and count them. Once this is done, I am to print out a histogram with asterisks. the ranges are------bin 0. if score < 10.----bin 1. if score >= 10 but < 20......ect. Then the output; 9| ** 8|* 7| *** and so on until 0. The asterisks need to resemble the amount of numbers that fit within the specified range and stored in an array element. I am stuck on this part and we can't use vectors, or any #includes other than , , and using namespace std; Any advice is great. I have my cout in the main to make sure my math was correct.
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
/*int histo(int x);*/
double dev(int count, int* scores, double mn);
double mean(int count, int* stats);
int main()
{
int scores[101];
int count = 0;
int bin[10];
double mn;
cout << "Enter a score (-1 to stop): ";
do
{
cin >> scores[count++];
} while (scores[count - 1] != -1);
count--;
mn = mean(count, scores);
cout << "mean: " << mean(count, scores) << endl;
cout << "dev: " << dev(count, scores, mn) << endl;
system("pause");
return 0;
}
int histo(int* scores)
{
int bins[10]{};
int counter = 0;
for (int i = 0; i < *scores; i++)
{
if (*scores < 10)
{
bins[counter++];
}
else if (*scores >= 10 && *scores < 20)
{
bins[counter++];
}
else if (*scores >= 20 && *scores < 30)
{
bins[counter++];
}
else if (*scores >= 30 && *scores < 40)
{
bins[counter++];
}
else if (*scores >= 40 && *scores < 50)
{
bins[counter++];
}
else if (*scores >= 50 && *scores < 60)
{
bins[counter++];
}
else if (*scores >= 60 && *scores < 70)
{
bins[counter++];
}
else if (*scores >= 80 && *scores < 90)
{
bins[counter++];
}
else if (*scores >= 90)
{
bins[counter++];
}
for (int j = 0; j < )
}
}
double dev(int count, int* scores, double mn)
{
double x = 0;
double y = 0;
double d = 0;
for (int i = 0; i < count; i++)
{
x = pow(scores[i] - mn, 2);
y += x;
}
d = sqrt(y / count);
return d;
}
double mean(int count, int* scores)
{
double total = 0;
for (int i = 0; i < count; i++)
{
total += scores[i];
}
return total / count;
}
I know I went over kill with the if statements. This is just where I am unsure of what to do.
for (int i = 0; i < *scores; i++)
Ok, so scores is a pointer to an array. By de-referencing it you get the value of the first element of that array, but not the count/size of it.
if (*scores < 10)
{
bins[counter++];
}
else if (*scores >= 10 && *scores < 20)
{
bins[counter++];
}
bins[counter++] will go out of bounds very soon with this setup, since your scores array is > 10 (not taking the undefined behaviour into account). Also the else statement already implies that *scores >= the previous statement.
So the solution to this will be something like this, assuming you want to increase the bin on index n every time:
int histo(int *scores, int scoreCount)
{
int bins[9]{};
for (int i = 0; i < scoreCount; i++)
{
if (scores[i] < 10)
{
bins[0]++;
}
else if (scores[i] < 20)
{
bins[1]++;
}
else if (scores[i] < 30)
{
bins[2]++;
}
else if (scores[i] < 40)
{
bins[3]++;
}
else if (scores[i] < 50)
{
bins[4]++;
}
else if (scores[i] < 60)
{
bins[5]++;
}
else if (scores[i] < 70)
{
bins[6]++;
}
else if (scores[i] < 90)
{
bins[7]++;
}
else
{
bins[8]++;
}
}
// Do stuff with your bins
}
I believe you're making the histo function a lot more complicated than it needs to be.
void histo(int* scores)
{
int bins[10] = {0};
for (int i = 0; scores[i] != -1 ; i++)
{
int index = scores[i]/10;
bins[index]++;
}
for(int i = 0; i < 10; i++){
cout << i + 1;
for(int j = 0; j < bins[i]; j++){
cout << "*";
}
cout << endl;
}
}

How do I get this random number generator out of an infinite loop?

I have an assignment for school where I need to create a lottery program. It is supposed to allow the user to input six numbers and then generate six random numbers for comparison. I got the inputs working, but I have encountered a problem where the random number generator (located in the while loop) is stuck in an infinite loop, and I have absolutely no idea what is causing it since I have never had an infinite loop in any previous programs. If someone could please look through the code and possibly establish what is wrong, I would greatly appreciate it.
#include<iostream>
#include<time.h>
using namespace std;
void randomizeSeed();
int randomRange(int min, int max);
int getInteger();
int main()
{
randomizeSeed();
const int minNumber = 1;
const int maxNumber = 49;
const int Size = 6;
int luckyNumbers[6] = {};
int randomNumber = randomRange(minNumber, maxNumber);
int winningNumbers[6] = {};
cout << "Enter six numbers between 1 and 49...\n";
{
for (int i = 0; i < Size; i++)
{
luckyNumbers[i] = getInteger();
}
for (int i = 0; i < Size; i++)
{
for (int i = 0; i < Size - 1; i++)
{
if (luckyNumbers[i] > luckyNumbers[i + 1])
{
int temp = luckyNumbers[i];
luckyNumbers[i] = luckyNumbers[i + 1];
luckyNumbers[i + 1] = temp;
}
}
}
cout << "Lucky Numbers: ";
for (int i = 0; i < Size; i++)
{
cout << luckyNumbers[i] << " ";
}
cout << "\n";
cout << "Press any button to see the Winning Numbers.\n";
system("pause");
bool exist = true;
while (exist == true)
{
int count = 0;
cout << "Winning Numbers: ";
for (int j = 0; j < 6; j++)
{
winningNumbers[j] = randomRange(1, 49);
cout << winningNumbers[j] << " ";
system("pause");
}
}
}
}
void randomizeSeed()
{
srand(time(NULL));
}
int randomRange(int min, int max)
{
int randomValue = rand() % (max + 1 - min) + min;
return randomValue;
}
int getInteger()
{
int value = 0;
while (!(cin >> value) || (value >= 50) || (value <= 0))
{
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
}
return value;
}
for (int i = 0; i < Size; i++)
for (int i = 0; i < Size - 1; i++)
if (luckyNumbers[i] > luckyNumbers[i + 1])
{
int temp = luckyNumbers[i];
luckyNumbers[i] = luckyNumbers[i + 1];
luckyNumbers[i + 1] = temp;
}
You have two loops and they both use i. You probably mean to use the second loop with another variable name, for example:
for (int i = 0; i < Size; i++)
{
for (int k = 0; k < Size - 1; k++)
{
if (luckyNumbers[i] > luckyNumbers[k + 1])
{
int temp = luckyNumbers[i];
luckyNumbers[i] = luckyNumbers[k + 1];
luckyNumbers[k + 1] = temp;
}
}
}
If you set your compiler warning level to 4 then compiler should warn you about these errors. Try to resolve all compiler warnings.