Notes![what is notes.io? What is notes.io?](/theme/images/whatisnotesio.png)
![]() ![]() Notes - notes.io |
This program creates random 1D array
And counts how many prime number
it includes.
INPUT: Size of array
start and end point of an interval
by Dr.MuZO, 9.11.2017, Thursday evening
VERSION : 0.0.01
*/
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int * CreateRandomArray(int size, int start, int end){
int i; // loop counter
int * arr; // random array
arr = (int *)malloc(size*sizeof(int));
//Check in there exixst enough memory
if (arr==NULL) exit(0);
srand((unsigned)time(NULL));
for(i=0;i<size;i++){
*(arr+i) = rand()%(end-start+1)+start;
}
return arr;
}
int IsPrime(int number){
//very very naive approach
int i;
//Brute Force Approach..
//Check every number up to n/2
for(i=2;i<number/2;i++)
if(number%i==0) return 0;
return 1;
}
void SelectionSort(int * arr, int size){
int i,j; //loop counter
int max, maxindex;
int temp;
for(j=0;j<size-1;j++){
max=-1;
for(i=0;i<size-j;i++){
if(arr[i]>max){
max = arr[i];
maxindex = i;
}
}
temp = arr[size-j-1];
arr[size-j-1] = arr[maxindex];
arr[maxindex]=temp;
}
}
void BubbleSort(int * arr, int size){
int i,j; //loop counter
int temp;
for(i=0;i<size-1;i++){
for(j=0;j<size-i-1;j++){
if(arr[j+1]<arr[j]){
temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}//end of inner loop
}//end of outer loop
}//end of bubblesort function
void PrintArray(int * arr, int size){
int i; //loop counter
for(i=0;i<size;i++){
if(i!=0 && i%25==0) printf("%dn ",*(arr+i));
else printf("%d ",*(arr+i));
}
printf("n");
}
int main(){
int * TestArray;
int SIZE, begin, final;
int i; //loop counter
int counter = 0;
printf("Please enter size of your arrayn");
scanf("%d",&SIZE);
printf("Please enter begin point of your intervaln");
scanf("%d",&begin);
printf("Please enter end point of your intervaln");
scanf("%d",&final);
TestArray = CreateRandomArray(SIZE, begin, final);
for(i=0;i<SIZE;i++)
if(IsPrime(TestArray[i])) counter++;
//Print Results
printf("There are %d ",counter,"prime number in your list");
printf("Your arrary sortedn");
BubbleSort(TestArray,SIZE);
PrintArray(TestArray,SIZE);
![]() |
Notes.io is a web-based application for taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000 notes created and continuing...
With notes.io;
- * You can take a note from anywhere and any device with internet connection.
- * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
- * You can quickly share your contents without website, blog and e-mail.
- * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
- * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.
Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.
Easy: Notes.io doesn’t require installation. Just write and share note!
Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )
Free: Notes.io works for 12 years and has been free since the day it was started.
You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;
Email: [email protected]
Twitter: http://twitter.com/notesio
Instagram: http://instagram.com/notes.io
Facebook: http://facebook.com/notesio
Regards;
Notes.io Team