The assignment is as followed and is coded in Keil which is an ide for c.
unsigned short data[100] = {
58473, 33594, 38638, 26741, 13018, 29262, 16377, 12354, 46079,
57240, 48949, 34054, 16212, 58485, 6198, 38678, 22525, 51012,
43489, 8861, 54291, 21524, 7166, 22698, 39899, 27113, 30443,
14888, 27935, 40035, 48710, 18067, 36008, 12644, 56319, 15852,
54685, 61789, 57030, 4763, 10655, 24656, 60363, 23712, 28474,
31274, 39647, 56166, 8219, 47413, 22201, 3129, 25630, 36027,
4499, 56525, 32743, 9380, 22102, 51009, 16309, 16589, 26322,
65279, 22780, 26002, 41101, 26082, 13389, 59504, 15784, 33416,
57970, 8519, 57819, 34406, 40864, 31575, 52154, 60214, 39910,
43107, 64825, 40284, 60148, 27287, 38245, 49930, 54062, 50668,
30553, 27904, 38960, 49407, 10508, 62147, 33019, 3047, 33750, 18024};
Write functions to preform the following operations on the array. You must pass the array to functions in order to receive full credit.
Find the index of the largest number, FindInexOfLargest().
Remove a specified entry, given an index, in the array, RemoveEntry(). Note that data appearing in the array after the removed entry is to be shifted to the left, so that data[i] becomes data[i+1].
Remove the largest entry in an array, RemoveLargestEntry(). The array is mutated to remove the largest entry. The value of the largest entry is returned by the function.
Remove the second largest entry in an array, RemoveSecondLargestEntry(). The function should return the numerical value of the second largest entry in an array, and remove that value.
The functions written for 3 and 4 should use the functions written in 1 and 2. For full credit, no loops should appear in the functions written in part 3 and 4.
This is my code that isn't working right and I can't find out where the mistake is. It's a logical error somewhere I just don't know where.
unsigned short int data[100] = {
58473, 33594, 38638, 26741, 13018, 29262, 16377, 12354, 46079,
57240, 48949, 34054, 16212, 58485, 6198, 38678, 22525, 51012,
43489, 8861, 54291, 21524, 7166, 22698, 39899, 27113, 30443,
14888, 27935, 40035, 48710, 18067, 36008, 12644, 56319, 15852,
54685, 61789, 57030, 4763, 10655, 24656, 60363, 23712, 28474,
31274, 39647, 56166, 8219, 47413, 22201, 3129, 25630, 36027,
4499, 56525, 32743, 9380, 22102, 51009, 16309, 16589, 26322,
65279, 22780, 26002, 41101, 26082, 13389, 59504, 15784, 33416,
57970, 8519, 57819, 34406, 40864, 31575, 52154, 60214, 39910,
43107, 64825, 40284, 60148, 27287, 38245, 49930, 54062, 50668,
30553, 27904, 38960, 49407, 10508, 62147, 33019, 3047, 33750, 18024};
// ***** 2. Global Declarations Section *****
// FUNCTION PROTOTYPES: Each subroutine defined
unsigned short int FindIndexOfLargest(unsigned short int class[], unsigned short int start, unsigned short int end);
unsigned short int RemoveEntry(unsigned short int class[], unsigned short int size, int position);
unsigned short int RemoveLargestEntry(unsigned short int class[], unsigned short int size);
unsigned short int RemoveSecondLargest(unsigned short int class[], unsigned short int start, unsigned short int end);
// ***** 3. Subroutines Section *****
int main (void) {
printf("Largest: %u\n", FindIndexOfLargest(data, 0, 100));
printf("Removing: %u\n", RemoveEntry(data, 100, 7));
printf("Removing Largest: %u\n", RemoveLargestEntry(data, 100));
printf("Removing Second Largest: %u\n", RemoveSecondLargest(data, 0, 100));
}
unsigned short int FindIndexOfLargest(unsigned short int class[],unsigned short int start, unsigned short int end){
unsigned short int largest,i;
largest = 0; // smallest possible value
for(i=0; i<end; i++){
if(class[i] > largest){
largest = class[i]; // new maximum
}
}
return (class[i]);
}
unsigned short int RemoveEntry(unsigned short int class[], unsigned short int size, int position) {
unsigned short int c;
for (c = (position - 1) ; c < (size - 1) ; c++ ) {
class[c] = class[c+1];
}
return class[c];
}
unsigned short int RemoveLargestEntry(unsigned short int class[], unsigned short int size) {
return RemoveEntry(class, size, FindIndexOfLargest(class, 0, 100));
}
unsigned short int RemoveSecondLargest(unsigned short int class[], unsigned short int start, unsigned short int end) {
unsigned short int Largest = FindIndexOfLargest(class, start, end);
unsigned short int firstLarge = FindIndexOfLargest(class, start, (Largest-1));
unsigned short int secondLarge = FindIndexOfLargest(class, (Largest+1), end);
if (firstLarge > secondLarge) {
return RemoveEntry(class, end, firstLarge);
}
else {
return RemoveEntry(class, end, secondLarge);
}
}
There is one mistake in FindIndexOfLargest function Change it to:
unsigned short int FindIndexOfLargest(unsigned short int class[],unsigned short int start, unsigned short int end){
unsigned short int largest,i;
largest = 0; // smallest possible value
for(i=0; i<end; i++){
if(class[i] > largest){
largest = class[i]; // new maximum
}
}
return (largest); //NOTE CHANGE HERE:Return largest number
}
As the function name suggest if you want to return the index of largest number then change it to:
unsigned short int FindIndexOfLargest(unsigned short int class[],unsigned short int start, unsigned short int end){
unsigned short int largest,i;
largest = 0; // smallest possible value
for(i=0; i<end; i++){
if(class[i] > largest){
largest = i; // NOTE CHANGE HERE:new maximum index
}
}
return (largest); //NOTE CHANGE HERE:Return largest index
}
As pointed out by Vagish There is a mistake in the function FindIndexOfLargest you are doing this
unsigned short int FindIndexOfLargest(unsigned short int class[],unsigned short int start, unsigned short int end){
unsigned short int largest,i;
largest = 0; // smallest possible value
for(i=0; i<end; i++){
if(class[i] > largest){
largest = class[i]; // new maximum
}
}
return (largest); // This is returning the largest no rather than returning the index
}
so do it like this
unsigned short int FindIndexOfLargest(unsigned short int class[],unsigned short int start, unsigned short int end){
unsigned short int largest,i,index;
largest = 0; // smallest possible value
for(i=0; i<end; i++){
if(class[i] > largest){
largest = class[i]; // new maximum
index=i;
}
}
return (index); //This is returning the index of the largest
}
There is also the problem in the Remove Entry function do it like this
unsigned short int RemoveEntry(unsigned short int class[], unsigned short int size, int position) {
unsigned short int c;
for (c =position; c <size;++c ) {
class[c] = class[c+1];
}
size-=1;
return size; //Return size of new array after removing the data
}
Well then there remains the last function, please try to find the logical error there and if not able let us know :)
Hope these answer helps
unsigned short int FindIndexOfLargest(unsigned short int tempArr[],unsigned short int start, unsigned short int end){
unsigned short int largest,i;
largest = 0; // smallest possible value
for(i=0; i<end; i++){
if(tempArr[i] > tempArr[largest]){
largest = i; // new maximum
}
}
return (largest); //Change return value class[i] to largest;
}
void RemoveEntry(unsigned short int class[], unsigned short int size, int position) {
unsigned short int c;
for (c = (position - 1) ; c < (size - 1) ; c++ ) {
class[c] = class[c+1];
}
//return class[c]; <<< array pass by pointer,so no need to return; *change unsigned short int to void.
void RemoveLargestEntry(unsigned short int tempArr[], unsigned short int size) {
RemoveEntry(class, size, FindIndexOfLargest(tempArr, 0, 100)); // This is the same with RemoveEntry function;
}
unsigned short int RemoveSecondLargest(unsigned short int tempArr[],unsigned short int start, unsigned short int end) {
unsigned short int Largest,PositionLargest,SecondLargest,PositionSecondLargest;
Largest = tempArr[FindIndexOfLargest(tempArr[],0,end)];
RemoveLargestEntry(tempArr,end); /*Remove largest and sencond largest number in the Array */
SecondLargest = FindIndexOfLargest(tempArr,0,end);
RemoveLargestEntry(tempArr,end-1);
tempArr[99] = Largest; // Add number at the end of the array;
return(SecondLargest);
}
Function 4 is only for your provide array. If the situation where the number in the array is less than 100(e.g 30),function 4 will be useless.
I have a function which expects a 8 bytes long unsigned char.
void f1(unsigned char *octets)
{
unsigned char i;
for (i=0;i<8;i++)
printf("(%d)",octets[i]);
}
This is how I use it while I have one 64bit integer:
unsigned long long ull = 1;
f1((unsigned char *) &ull);
(it uses the machine's native endianness.)
My question is, if instead of having 1x64bit integer, I have 2x32bit integers - is there a way to combine them efficiently as an input for this specific function?
unsigned long int low = 1;
unsigned long int high = 0;
Does a union work portably? If so, it's a good approach...
union {
struct {
unsigned char CharArray[8];
} ub;
struct {
unsigned long int IntArray[2];
} ul;
unsigned long long ull;
} Foo;
You could just put them in an array:
unsigned long int lohi[2] = {1, 0};
f1((unsigned char *) lohi);
edit: Using existing variables:
unsigned long int lohi[2] = {lo, hi};
Typecast, bitshift and do bitwise or.
unsigned long int low = 1;
unsigned long int high = 0;
unsigned long long ull = (unsigned long long) high << 32 | low;
You can use a combination of union and struct to keep your namings and not using arrays.
union {
struct {
unsigned long int low = 0;
unsigned long int high = 1;
};
unsigned long long int ull;
};
Use low and high as you would do, and use ull when calling f1. But notice that writing it this way, you assume a little endian ordering.
Also note that on Linux anh other UNIXes, in 64 bits mode, both long int and long long int are 64 bits (only int is 32 bits). Of what I know, only Windows has long int as 32 bits in 64 bits mode.
Different way of looking at it:
void f1( unsigned char* octet )
{
f2( octet, octet + 4 );
}
void f2( unsigned char* quad1, unsigned char *quad2 )
{
unsigned char i;
for (i=0;i<4;i++)
printf("(%d)",quad1[i]);
for (i=0;i<4;i++)
printf("(%d)",quad2[i]);
}
Works better in C++ when both functions can have the same name.