You are viewing our Forum Archives. To view or take place in current topics click here.
Trouble with C programming
Posted:

Trouble with C programmingPosted:

Almac14
  • 2 Million
Status: Offline
Joined: Mar 04, 201212Year Member
Posts: 187
Reputation Power: 7
Status: Offline
Joined: Mar 04, 201212Year Member
Posts: 187
Reputation Power: 7
My assignment: You are given a file with 20 integers. Your task is to select and print the three largest numbers with NO sorting and NO arrays. It is possible that the largest numbers are repeated you should find all occurrences of the three largest numbers. You must accomplish this task without sorting the file and without using arrays for any purpose. The reading of the numbers, the comparisons, the printing of the largest numbers should all be in separate functions. You MUST employ pass by value for this problem.

My problem: I have no idea how to start or even what to do. Any help is very much appreciated
#2. Posted:
Xaldin
  • TTG Addict
Status: Offline
Joined: Oct 09, 201013Year Member
Posts: 2,358
Reputation Power: 106
Status: Offline
Joined: Oct 09, 201013Year Member
Posts: 2,358
Reputation Power: 106
something like if the next integer is bigger than the last store it, else skip it. This will leave you with the biggest int. That can be modified to get the top 3, would hate to do your assignment for you.
#3. Posted:
Almac14
  • Challenger
Status: Offline
Joined: Mar 04, 201212Year Member
Posts: 187
Reputation Power: 7
Status: Offline
Joined: Mar 04, 201212Year Member
Posts: 187
Reputation Power: 7
Xaldin wrote something like if the next integer is bigger than the last store it, else skip it. This will leave you with the biggest int. That can be modified to get the top 3, would hate to do your assignment for you.


Not asking you to do my assignment, just an easy way for me to understand and start my code. I have a good idea of what I need to do just not sure how to code it.
#4. Posted:
-Deano
  • PC Master Race
Status: Offline
Joined: Aug 19, 201013Year Member
Posts: 5,238
Reputation Power: 532
Status: Offline
Joined: Aug 19, 201013Year Member
Posts: 5,238
Reputation Power: 532
Declare three integers to hold your largest values.

Go through the list of integers and if they are bigger than the current value, replace them in our list of biggest numbers.
I'll give you the pseudo code for what you should be doing but it's your job to still code it.


declare three integers to hold the three largest values and initialise them to 0.
while (you have not reached the end of the file) {
   Read the next integer.
   if the integer is larger than our 3rd number {         // it is at least the 3rd biggest number so far.
      if the integer is larger than our 2nd number {       // it is at least the 2nd biggest number so far.
         if the integer is larger than our 1st number {  // it is the biggest number so far.
            3rd number = current 2nd number            // move our current numbers down in the line so that we can insert our new biggest number.
            2nd number = current 1st number
            1st number = the new integer
         } else {                              // it is the 2nd biggest number so far.
            3rd number = current 2nd number            // move the current 2nd number down to 3rd place.
            2nd number = the new integer            // insert our new 2nd biggest number.
         }
      } else {                                 // it is the 3rd biggest number so far.
         3rd integer = the new integer               // insert our new 3rd biggest number.
      }
   }
}
output the three largest numbers


That is the basic pseudo code for the problem. You will need to implement the reading of the numbers, as well as the comparisons and final output, into their own functions and call them whilst passing the current new integer as a parameter.

Let me see what progress you have made and I will help you further if you still need it.
#5. Posted:
Xaldin
  • 2 Million
Status: Offline
Joined: Oct 09, 201013Year Member
Posts: 2,358
Reputation Power: 106
Status: Offline
Joined: Oct 09, 201013Year Member
Posts: 2,358
Reputation Power: 106
Almac14 wrote
Xaldin wrote something like if the next integer is bigger than the last store it, else skip it. This will leave you with the biggest int. That can be modified to get the top 3, would hate to do your assignment for you.


Not asking you to do my assignment, just an easy way for me to understand and start my code. I have a good idea of what I need to do just not sure how to code it.

I know that but start with reading in the file into three variables like Deano said, try to work through it. If you get stuck we'll point you in the right direction
#6. Posted:
Almac14
  • Challenger
Status: Offline
Joined: Mar 04, 201212Year Member
Posts: 187
Reputation Power: 7
Status: Offline
Joined: Mar 04, 201212Year Member
Posts: 187
Reputation Power: 7
Alright so I managed to work on this all day. I made a lot of progress and now my only thing I'm stumped on is probably really easy and I'm probably overthinking it a lot. But I can;t figure out where to print for the 3 largest numbers or what function to call when I do.

My code:

#include <stdio.h>

int count1;
int count2;
int count3;
int compareNum(int input, int i);


int readNum(int i)
{
    int num;
    int temp;
   
    FILE *file;
    file = fopen("numbers.txt", "r");
    for(i = 0; i < 20; i++)
    {
        fscanf(file, "%d", &num);
        temp = compareNum(num, i);
    }
    fclose(file);
    return temp;
}

int compareNum(int input, int i)
{
    int first = 0;
    int second = 0;
    int third = 0;
   
    if(input>first)
    {
        count1 = 0;
   
        if(input>second)
        {
            count2 = 0;
            if(input>third)
            {
                count3=0;
                third = second;
                second = first;
                first = input;
            }
            else if(input==third)
            {
                count3++;
            }
            else if(input == second)
            {
                count2++;
            }
            else
            {
                third = second;
                second = input;
            }
        }
        else if(input == first)
        {
            count1++;
        }
        else
        {
            third = input;
        }
    }
   
    //checks for max, mid, and low
    if(i == 1)
    {
        return first;
    }
    else if(i == 2)
    {
        return second;
    }
    else
    {
        return third;
    }
}



int main()
{
    int first;
    int second;
    int third;
   
    first = readNum(1);
    second = readNum(2);
    third = readNum(3);
   
    //printf("First: %d \n", first);
}

#7. Posted:
-Deano
  • Spooky Poster
Status: Offline
Joined: Aug 19, 201013Year Member
Posts: 5,238
Reputation Power: 532
Status: Offline
Joined: Aug 19, 201013Year Member
Posts: 5,238
Reputation Power: 532
I can see you have given it a try so here's my method of implementing the solution.
You will have to do the file handling yourself as I have not done files within C before.
This is my implementation with more Java-based language (i.e. System.out.println() ) but I'm sure you get the idea of it.



public String readNumber() {
   // Get next integer from the file.
   // If there is one, return it as a String.
   // If there is not one, return "EOF".
}

public boolean checkThird(int newIntIn) {
   if (newIntIn > third) {
      return true;
   }
   return false;
}

public boolean checkSecond(int newIntIn) {
   if (newIntIn > second) {
      return true;
   }
   return false;
}

public boolean checkFirst(int newIntIn) {
   if (newIntIn > first) {
      return true;
   }
   return false;
}

public void printNumbers() {
   System.out.println("1st: " + first);
   System.out.println("2nd: " + second);
   System.out.println("3rd: " + third);
}

public static void main(String args[]) {
   int third = 0;
   int second = 0;
   int first = 0;
   int newInt = 0;
   String temp = "";

   for (int count = 0; count < 20;count++) {
      temp = readNumber(count); // Get the next integer (on line count)
      if (!(temp.Equals("EOF"))) { // If there is a valid integer
         newInt = Integer.ParseInt(temp); // Convert the string to an Integer.
         
         if (checkThird(newInt)) {
            if (checkSecond(newInt)) {
               if (checkFirst(newInt)) {
                  // newInt is the largest number so far.
                  third = second;
                  second = first;
                  first = newInt;
               } else {
                  // newInt is the second largest number so far.
                  third = second;
                  second = newInt;
               }
            } else {
               // newInt is the third largest number so far.
               third = newInt;
            }
         }
      }
   }
}

Jump to:
You are viewing our Forum Archives. To view or take place in current topics click here.