I want give array of numberes and find how many of them is prime number,but program give me zero
int Prime_complete(int a[],int len){
int sum=0;
int m,flag;
for(int i=0 ; i<len ; i++){
m=a[i]/2;
for(i = 2; i <= m; i++){
if(a[i] % i == 0){
flag=1;
break;
}
}
if (flag==0) {
sum++;
}
}
return sum;
}
As mentioned already in comments, you need to fix your flag variable to reset each iteration. Right now your code finds a number that isn't prime and then the flag is never reset.
#include <iostream>
using namespace std;
int Prime_complete(int a[],int len)
{
int sum = 0;
int flag = 1;
for(int i = 0; i < len; i++)
{
for (int j = 2; j < a[i]/2; j++)
{
if (a[i] % j == 0)
{
flag = 1;
}
}
if (flag == 0)
{
sum++;
}
flag = 0;
}
return sum;
}
int main()
{
int numbers[13] = {3, 5, 7, 11, 13, 17, 19, 23, 29, 4, 6, 8, 10};
cout << Prime_complete(numbers,13);
return 0;
}
You could also improve your prime checking by only checking values up to the sqrt(a[i]) but that would only be a minor improvement in this case.
Related
Question: Find unique elements from the given array and print them
I have dry run below given code many times but i am not getting what is the problem in the code.
#include <iostream>
using namespace std;
void findingUniqueElement(int array[], int size)
{
int brray[100];
int count = 0;
bool equal = 0;
for (int i = 0; i < size; i++)
{
equal = 0;
int value = array[i];
for (int j = 0; j < size; j++)
{
if (i != j && value == array[j])
{
equal = 1;
break;
}
}
if (equal == 0)
{
brray[i] = value;
count++;
}
}
for (int i = 0; i < count; i++)
{
cout << brray[i] << " ";
}
}
int main()
{
int arr[6] = {1, 2, 2, 5, 3, 7};
findingUniqueElement(arr, 6);
}
i was expecting as an output
1 5 3 7
but when i run the code, getting as output
1 1877357483 1878039440 5
Welcome to SO! Here is the code modified by me:
#include <cstring>
#include <iostream>
using namespace std;
void findingUniqueElement(int array[], int size) {
int brray[100];
memset(brray, 0, 100); // modified here
int count = 0;
bool equal = 0;
for (int i = 0; i < size; i++) {
equal = 0;
int value = array[i];
for (int j = 0; j < size; j++) {
if (i != j && value == array[j]) {
equal = 1;
break;
}
}
if (equal == 0) {
brray[i] = value;
count++;
}
}
for (int i = 0; i < size; i++) { // modified here
if (brray[i] != 0) // modified here
cout << brray[i] << " ";
}
}
int main() {
int arr[6] = {1, 2, 2, 5, 3, 7};
findingUniqueElement(arr, 6);
}
I modified three places in your code:
You should initialize brray when you declare it, the strange values output are because they were in the memory when you defined brray, you should clean the memory before you use it.
The way you store unique number in brray is not very correct, when array[i] is not unique, you will add i, which leave 0 at brray[i], so to fit your code, you should make i from 0 to size. To get more clear about what I'm talking about, you can check the memory of brray.
Since I assume there is no 0 in your test data, so if there is a 0 in barray, you know it is not a valid value, just skip it. Also, you can use a more elegant way to do this, like #Anand Sowmithiran commented:
int brray[100];
memset(brray, 0, 100);
int count = 0;
for (/* conditions... */) {
// if there is a unique number
brray[count++] = unique_number;
}
for (int index = 0; index < count; index++) {
cout << brray[index] << " ";
}
Hope my answer is helpful!
This is a code I have taken from GeeksForGeeks. I am trying to understand the code.
In the line in the function deleteEle which says if(i==n) return n, how could i become n if it is running for < n times in the for loop just above it?
#include <iostream >
#include <cmath>
using namespace std;
int deleteEle(int arr[], int n, int x)
{
int i = 0;
for(i = 0; i < n; i++)
{
if(arr[i] == x)
break;
}
cout << i << endl;
if(i == n)
cout<<i;
return n;
for(int j = i; j < n - 1; j++)
{
arr[j] = arr[j + 1];
}
return n-1;
}
int main() {
int arr[] = {3, 8, 12, 5, 6}, x = 13, n = 5;
cout<<"Before Deletion"<<endl;
for(int i=0; i < n; i++)
{
cout<<arr[I]<<" ";
}
cout<<endl;
n = deleteEle(arr, n, x);
cout<<"After Deletion"<<endl;
for(int i=0; i < n; i++)
{
cout<<arr[I]<<" ";
}
}
how could I become n if it is running for < n times in the for loop just above it?
Because it may happen that the if condition arr[i] == x inside the preceding for loop is never satisfied which in turn means that the break is never executed in which case the last iteration will increment i so that i becomes equal to n.
After the last iteration of this loop (if arr[i] != x for all i in range [0, n]):
for(i = 0; i < n; i++)
{
if(arr[i] == x)
break;
}
i will equal n if break is not executed.
Question is given that we have to find the duplicate number in an array. Can someone please tell me why this code is not working?
int main()
{
int arr[] = {12,13,14,15,15,10};
int size = sizeof(arr)/sizeof(int);
int n;
for (int i = 0; i < size; i++)
{
for (int j = 1; j < size; j++)
{
if (arr[i] == arr[j])
{
n = arr[i];
break;
}
}
}
cout<<n;
return 0;
}
Please tell me the problem.
You need to let j start from i+1, or it may match itself as the duplicate number.
int main()
{
int arr[] = {12, 13, 14, 15, 15, 10};
int size = sizeof(arr) / sizeof(int);
int n;
for (int i = 0; i < size; i++)
{
for (int j = i + 1; j < size; j++)
{
if (arr[i] == arr[j])
{
n = arr[i];
break;
}
}
}
cout << n;
return 0;
}
Your outer for loop doesn't break when duplicate is found. You can use another variable and set it to true if duplicate element is found.
int main()
{
int arr[] = {12, 13, 14, 15, 15, 10};
int size = sizeof(arr)/sizeof(int);
int n = 0;
bool duplicate_found = false;
for (int i = 0; i < size; i++)
{
for (int j = i + 1; j < size; j++)
{
if (arr[i] == arr[j])
{
n = arr[i];
duplicate_found = true;
}
}
if (duplicate_found) break;
}
cout << n;
return 0;
}
Don't use goto statement here. It's a bad practice to use goto. And as #schtonn suggested start your inner loop from i+1.
break takes you out of the inner loop only. If you want to break out of both loops, you need goto statement.
I was tinkering with C++ and I was like let's make a sorting algorithm :)
I did something, but it didn't sort the array but instead the array was overwritten with the max number
I don't know where is my mistake because I ran the algorithm on paper (don't ask) and it was correct.
I tried every modification possible.
any help?
#include <iostream>
#include <cstring>
using namespace std;
int main()
{
int mn = 0, mx = 0;
int a[] = { 4, 8, 5, 2, 6, 9, 0, 3, 1, 7 };
for (int i = 0; i < 10; i++)
{
mn = a[i]; mx = a[i];
for (int j = i; j < 10 - i; j++)
{
mn = min(a[j], mn);
mx = max(a[j], mx);
}
swap(a[i], mn);
swap(a[10-1-i], mx);
}
for(int i=0;i<10;i++)
{
cout<<a[i]<<" ";
}
}
You are not swapping array elements, but basically you write the min/max values to the corresponding places within the array. Their old value is simply overwritten. You need to track the positions of the min/max elements and swap accordingly, e.g. swap(a[i], a[min_pos]). Additionally, you could run your outer loop up until the middle of the array is reached as you put two elements into their place on each iteration.
Here's the working code:
#include <iostream>
#include <cstring>
using namespace std;
int main()
{
int mn = 0, mx = 0;
int a[] = { 4, 8, 5, 2, 6, 9, 0, 3, 1, 7 };
for (int i = 0; i < 10 / 2; i++)
{
int min_pos = i, max_pos = i;
for (int j = i; j < 10 - i; j++)
{
if (a[j] < a[min_pos]) {
min_pos = j;
} else if (a[j] > a[max_pos]) {
max_pos = j;
}
}
int min_val = a[min_pos];
int max_val = a[max_pos];
swap(a[i], a[min_pos]);
swap(a[10-1-i], a[max_pos]);
a[i] = min_val;
a[10-1-i] = max_val;
}
for(int i=0;i<10;i++)
{
cout<<a[i]<<" ";
}
}
Note, you need to take care of 'special cases', e.g. when the min_pos and max_pos are at the ends of the interval - they would be swapped twice staying in their original positions.
Hi when running this small program, I appear to have an output of the memory addresses rather than the values stored inside them: any ideas? thanks.
#include "stdafx.h"
#include <iostream>
using namespace std;
int main()
{
int a[5] = { 1, 3, 5, 7, 9 };
int b[5] = { 2, 4, 6, 8, 10 };
int c[10] = {};
int j, i;
for(j = 0; j < 10; j++)
{
if (a[j] < b[j])
{
c[j] = a[j];
}
else
{
c[j] = b[j];
}
}
for (i= 0; i < 10; i++)
{
cout << c[i];
}
return 0;
}
Your conditions are wrong. a and b both have size 5, but you are iterating upto size 10.