2D array sort

Help, im using TC3.0 and i want to find the lowest value in the array below, on each row.i.e
so the first value will be 2 and next one will be 3.  i tried to work out the
answer by comparing the array to the first element but im getting 2 as the answer for each row!
i can figure out why this is happening


#include <stdio.h>
#include <conio.h>
int main()
{
      int row,col;
      int temp=0;
      int shortestJob;
      int machines [4] [5] ={
                                          {2,8,3,9,2},
                                          {3,9,8,7,6},
                                          {7,4,6,2,3},
                                          {4,6,3,5,6}
                                          
                                          };

      shortestJob=machines[0][0];
      clrscr();
      printf("%10s","Machine");
      for(col=0;col<5;col++)
            printf("%5d",col);
      printf("\n%-10s\n","Job");


      for(row=0;row<4;row++)
      {
            printf("%2d%8s",row,"");

            for(col=0;col<5;col++)
            {
                  printf("%5d",machines[row][col]);
                  if (machines [row][col] < shortestJob)
                  {
                        shortestJob=machines[row][col];
                  }


            }
            printf("\t%d",shortestJob);
            printf("\n");
      }
      printf("shortest job is %d",shortestJob);
return 0;
}

LVL 2
gerrymcdAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
niemeyerConnect With a Mentor Commented:

You've just forgotten to reset shortest job's value
between diferent rows.


#include <stdio.h>
#include <conio.h>

int main()
{
  int row,col;
  int temp=0;
  int shortestJob;
  int machines [4] [5] ={
    {2,8,3,9,2},
    {3,9,8,7,6},
    {7,4,6,2,3},
    {4,6,3,5,6}

  };

  shortestJob=machines[0][0];
  clrscr();
  printf("%10s","Machine");
  for(col=0;col<5;col++)
    printf("%5d",col);
  printf("\n%-10s\n","Job");
  for(row=0;row<4;row++)
    {
      /* You forgot this line */
      shortestJob=machines[row][0];
      printf("%2d%8s",row,"");
      for(col=0;col<5;col++)
        {
          printf("%5d",machines[row][col]);
          if(machines[row][col] < shortestJob)
            shortestJob=machines[row][col];
        }
      printf("\t%d",shortestJob);
      printf("\n");
    }
  printf("Shortest job is %d",shortestJob);
  return 0;

0
 
chensuCommented:
Is this homework? Why don't you debug it? It is very easy to figure out.

Hint: The program currently finds the lowest value in the 2D array. You need to assign ??? to shortestJob when each row is started.
0
 
GodSlayerCommented:
For each row set shortestJob equal to the first element in the row.  Like chensu already effectively told you.
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
chensuCommented:
An expert may be removed from EE by providing answers to school assignments. Check out the agreement.
0
 
gerrymcdAuthor Commented:
thanks for the ans.  who said this was a school assignmnet. ist part of a program im writting, i just wanted to make sure iwas on the right track.
0
 
niemeyerCommented:

Well, at this moment I can't read minds Mr Agreement... but some day I get there.

They can remove me if they want to... Maybe then I'll do something more usefull and profitable than wasting my time helping people in exchange for useless points.
0
All Courses

From novice to tech pro — start learning today.