DIR Return Create A Forum - Home
---------------------------------------------------------
Class H22
HTML https://classh22.createaforum.com
---------------------------------------------------------
*****************************************************
DIR Return to: exercises
*****************************************************
#Post#: 18--------------------------------------------------
קוד מקור 27.2
DIR By: Noam
Date: March 2, 2014, 2:45 am
---------------------------------------------------------
First Exercise, without sorting
--- Code ---
/*
* Author: Noam Chen
* Date: 27.2.14
*
* This program prompts the user to input integer
* values of array. The program calls a function which will
displays
* the smallest and greatest values of this array. It also
returns the value
* that occur the most in this array
*/
#include <stdio.h>
#define ARR_SIZE 10
int GetMostCommon (int* a, int size);
int Min(int* a, int size);
int Max(int* a, int size);
int Occur(int* a, int size);
int main()
{
int arr[ARR_SIZE], i;
/* get integers store in array*/
printf("Please enter a series of numbers:\n");
for (i = 0; i < ARR_SIZE; i++)
{
scanf("%d", arr+i);
}
GetMostCommon (arr, ARR_SIZE);
return 0;
}
int GetMostCommon (int* a, int size)
{
int min, max;
min = Min(a, size);`
max = Max(a, size);
printf ("min: %d\n", min);
printf ("max: %d\n", max);
return Occur(a, size);
}
/* this function finds the number that appears most times in
an unsorted array*/
int Occur (int* a, int size)
{
int appear_max, max_count, i, k, tmp_count;
/*go over array*/
appear_max = a[0];
max_count = 1;
for (i = 0; i < size; ++i)
{
/*avoid testing the max number again*/
if (a[i] == appear_max && i != 0)
{
continue;
}
/*initialize counter*/
tmp_count = 1;
/*go over rest of array*/
for (k = i + 1; k < size; ++k)
{
/*count similar*/
if (a[i] == a[k])
{
++tmp_count;
}
/*if there is no chance this number will appear more
times*/
if (size - k + tmp_count < max_count)
{
break;
}
}
/* did the new number appear more times*/
if (max_count < tmp_count)
{
max_count = tmp_count;
appear_max = a[i];
}
/*if there is no chance another number will have more
appearances*/
if (size - i < max_count)
{
break;
}
}
printf ("%d appeared the most times: %d\n", appear_max,
max_count);
return appear_max;
}
int Max(int* a, int size)
{
int i, max;
max = a[0];
for (i = 0; i < size; ++i)
{
if (max < a[i])
{
max = a[i];
}
}
return max;
}
int Min(int* a, int size)
{
int i, min;
min = a[0];
for (i = 0; i < size; ++i)
{
if (min > a[i])
{
min = a[i];
}
}
return min;
}
--- End Code ---
With sorting
--- Code ---
/*
* Author: Noam Chen
* Date: 27.2.14
*
* This program prompts the user to input integer values into
an array.
* It calls function sort which sorts so all even numbers come
first then all odd numbers, inner order maintained.
*
* Negative input is allowed. Returns number of even numbers
*
*/
#include <stdio.h>
#define ARR_SIZE 4
void EvenSort (int* a, int size);
void swap (int* x, int* y);
int main()
{
int arr[ARR_SIZE], i;
/* get integers store in array*/
printf("Please enter a series of numbers:\n");
for (i = 0; i < ARR_SIZE; i++)
{
scanf("%d", arr+i);
}
/* sort the array, even numbers first and then the odd*/
EvenSort(arr, ARR_SIZE);
/* debugging purposes
for (i = 0; i < ARR_SIZE; i++)
{
printf("%d ", arr[i]);
}
putchar('\n');
*/
/*sum the evebn numbers*/
i = 0;
while (arr[i] % 2 == 0)
{
++i;
}
printf("even numbers amount: %d\n", i);
return i;
}
/*sorts so all even numbers come first then all odd numbers,
inner order maintained.*/
void EvenSort (int* a, int size)
{
int i,tmp, swaps = 0;
/*go over the array*/
for (i = 0; i < size-1; ++i)
{
/*if odd bubble up until odd */
if (a[i] % 2 != 0 && a[i+1] % 2 == 0)
{
swap (&a[i], &a[i+1]);
++swaps;
}
/* Debug
for (tmp = 0; tmp < ARR_SIZE; tmp++)
{
printf("%d ", a[tmp]);
}
putchar('\n');
*/
/* only if there are no swaps- the array is sorted*/
if (i == size - 2 && swaps != 0)
{
swaps = 0;
i = -1; /* it will get incremented to index 0 */
--size; /*the top element is sorted*/
}
}
}
/*this fuctions swaps the valus pf the input addresses*/
void swap (int* x, int* y)
{
int tmp;
tmp = *x;
*x = *y;
*y = tmp;
}
--- End Code ---
A different counting function (Moshe's Algorythm):
--- Code ---
/* this function finds the number that appears most times in
the array*/
int MoshRoll (int* a, int size)
{
int appear_max, max_count, i, memi;
/*go over array*/
appear_max = a[0];
max_count = 1;
memi = 0;
for (i = 1; i < size; ++i)
{
/*if there is a value change*/
if (a[i] != a[i-1])
{
/*if count is bigger than current- remember it*/
if (max_count < i - memi)
{
max_count = i - memi;
appear_max = a[i-1];
}
/*remember the index*/
memi = i;
}
/*EOA case*/
else if (i == size - 1)
{
if (max_count < size - memi)
{
max_count = size - memi;
appear_max = a[i];
}
}
}
printf ("%d appeared the most times: %d\n", appear_max,
max_count);
return appear_max;
}
--- End Code ---
Even numbers sort, with regular swap. If I'll have some time
I'll try to add the right shift.
--- Code ---
/*
* Author: Noam Chen
* Date: 27.2.14
*
* This program prompts the user to input integer values into
an array.
* It calls function sort which sorts so all even numbers come
first then all odd numbers, inner order maintained.
*
* Negative input is allowed. Returns number of even numbers
*
*/
#include <stdio.h>
#define ARR_SIZE 4
void EvenSort (int* a, int size);
void swap (int* x, int* y);
int main()
{
int arr[ARR_SIZE], i;
/* get integers store in array*/
printf("Please enter a series of numbers:\n");
for (i = 0; i < ARR_SIZE; i++)
{
scanf("%d", arr+i);
}
/* sort the array, even numbers first and then the odd*/
EvenSort(arr, ARR_SIZE);
/* debugging purposes
for (i = 0; i < ARR_SIZE; i++)
{
printf("%d ", arr[i]);
}
putchar('\n');
*/
/*sum the evebn numbers*/
i = 0;
while (arr[i] % 2 == 0)
{
++i;
}
printf("even numbers amount: %d\n", i);
return i;
}
/*sorts so all even numbers come first then all odd numbers,
inner order maintained.*/
void EvenSort (int* a, int size)
{
int i,tmp, swaps = 0;
/*go over the array*/
for (i = 0; i < size-1; ++i)
{
/*if odd bubble up until odd */
if (a[i] % 2 != 0 && a[i+1] % 2 == 0)
{
swap (&a[i], &a[i+1]);
++swaps;
}
/* Debug
for (tmp = 0; tmp < ARR_SIZE; tmp++)
{
printf("%d ", a[tmp]);
}
putchar('\n');
*/
/* only if there are no swaps- the array is sorted*/
if (i == size - 2 && swaps != 0)
{
swaps = 0;
i = -1; /* it will get incremented to index 0 */
--size; /*the top element is sorted*/
}
}
}
/*this fuctions swaps the valus pf the input addresses*/
void swap (int* x, int* y)
{
int tmp;
tmp = *x;
*x = *y;
*y = tmp;
}
--- End Code ---
*****************************************************
Page 1 of 1