I need to input my own array and give its own elements, from that array i need to print the same one but if theres a number that is prime, it needs to switch it with the next number. Example: My array: 4 6 3 5 7 11 13 The new array: 4 6 5 3 11 7 13 Here prime numbers are, 3 5 7 and 13, but 13 doesnt have an element to switch itself, so it stays the same.
#include <stdio.h>
#define array 100
int prime(int b
)
{
int i;
for (i = 2; i <= b / 2; i )
{
if (b % i == 0)
{
return b; // not prime
}
break;
}
return b;
}
int main()
{
int n, i, a[array];
printf("How many elements does the array have?\n");
scanf("%d", &n);
printf("Put in %d elements from the array!\n", n);
for (i = 0; i < n; i )
{
scanf("%d", &a[i]);
}
printf("My array is: \n");
for (i = 0; i < n; i )
{
printf("%d ", a[i]);
}
for (i = 0; i < n; i )
{
if (prime(a[i]))
{
int temp;
temp = prost(a[i]);
prime(a[i]) == prime(a[i 1]);
}
}
printf("\nThe new array is:\n");
printf("%d ", prime(a[i]));
return 0;
}
I haven't learned pointers so is there a way without it or?
CodePudding user response:
First of all, you have a for loop that only makes one iteration because of a break keyword, also in main in a for loop with your swapping you need to assign return values from the prime function to variables, and in the same function, you should use singe '=' because you want to assign value but not to compare. Also in your same for loop, you should check if(prime(a[i 1])) so there won't be any segfaults.
CodePudding user response:
there are few things needs to modify
need to change function
prime
return type tobool
. since we are interest to check if array element is Prime. if array element is Prime, returnTrue
int prime(int b)
changed to
bool prime(int b)
also need to extend check if prime() function return true and if array index is not last element then only swap array element to next, else skip
if (prime(a[i]) == 1 && a[i-1] != n)
prost(a[i])
looks typo (I guess). corrected toa[i 1]
this is not optimized code, it just modified version of your code. if you have concern specific performance, please follow suggestion mentioned by chux - Reinstate Monica
code:
#include <stdbool.h>
#include <stdio.h>
#define array 100
bool prime(int b)
{
int i;
for (i = 2; i <= b / 2; i )
{
if (b % i == 0)
{
return false; // not prime
}
break;
}
return true;
}
int main()
{
int n, i, a[array];
int temp;
printf("How many elements does the array have?\n");
scanf("%d", &n);
printf("Put in %d elements from the array!\n", n);
for (i = 0; i < n; i )
{
scanf("%d", &a[i]);
}
printf("My array is: \n");
for (i = 0; i < n; i )
{
printf("%d ", a[i]);
}
for (i = 0; i < n; i )
{
if (prime(a[i]) == 1 && a[i] != a[n-1]) /* enter loop only array element is Prime number and it is not last element */
{
temp = a[i];
a[i] = a[i 1];
a[i 1] = temp;
}
a[i ];
}
printf("\nThe new array is:\n");
for (i = 0; i < n; i )
{
printf("%d ", a[i]);
}
return 0;
}
Output for above code: check out this link
How many elements does the array have?
7
Put in 7 elements from the array!
4
6
3
5
7
11
13
My array is:
4 6 3 5 7 11 13
The new array is:
4 6 5 3 11 7 13
...Program finished with exit code 0
Press ENTER to exit console.