Thread: Array dis-array :)

  1. #1
    Registered User
    Join Date
    Jan 2004
    Posts
    3

    Array dis-array :)

    Hello All...

    I've got a 2D Array that takes in inputs, each time the degree difficulty is to be scanned into the first slot of the Array..and then the scores added after that...
    then I go on to get the max and the min..
    Although it scans in the scores the difficulty gets ignored..
    any ideas on where I have slipped up....Gratefully appreciated



    Code:
    #include<stdio.h>
    
    int main (void)
    {
     
    int i,j;//array subscripts
    int counter=1;// this is my loop counter
    int counter_2=1;//this counter prints out the dive number to the user
    //this 2D Array holds the dive difficulty and the scores awarded by the five judges for diver 1
    // NOTE the Degree of difficulty is held in the first slot of each Array Row
    double murphy[6][3]; //Array declared and filled with zeros
    double max=0;//variable for max score
    double min=0;//variable for min score 
    // Program Splash Screen
    printf("*****************************************************************************\n");
    printf("*                                                                           *\n");
    printf("* Welcome to Splash Down                                                    *\n");
    printf("*                                                                           *\n");
    printf("*    Enter Your Divers Details                                              *\n");
    printf("*****************************************************************************\n");
    
    //this for loop fills the array with zero's
    for (i=0;i<6;i++)
    
    
    for (j=0;j<3;j++)
    {
    murphy[i][j]=0;
    }
    
    /*These for loops scan the the data into the 2D Array
    As our Array is like a matrix and we need two for loops to process
    the Rows and columes of the Array*/
    
    	for(i=0;i<3;i++)
    
    	{
    	//Ask the user to enter the dive difficulty (1-4)
    	printf("Enter Degree of  Difficulty For Dive %d Diver 1 (1-4) >>",counter_2);
    	scanf("%lf",&murphy[i][j]);
    	
    	for(j=1;j<6;j++)
    	
    		
    			{
    		
    		//As the user to enter the 5 dive scores for diver 1
    		printf("Enter Judge %d score for dive %d ",counter,counter_2);
    		scanf("%lf",&murphy[i][j]);
    	
    		counter+=1;
    		
    			}
    	counter=1;//reset the judges counter to 1 after each loop
    	counter_2+=1;//add 1 to the divers dive counter
    	
    
    	}
    //Search the array and find the max score 
    	for (i=0;i<3;i++)
    
    	for (j=0;j<6;j++)
    	
    		if (murphy[i][j]>max)
    {
    max=murphy[i][j];
    }
    	
    
    //Search the array and find the min score 
    	for (i=0;i<3;i++)
    
    	for (j=0;j<6;j++)
    	// if we find a score less than max then assign it to min
    		if (murphy[i][j]<max)
    {
    min=murphy[i][j];
    }
    
    //this is just a test print to make sure the data in the array is in correct
    for(i=0;i<3;i++)
    
    {
    	for(j=0;j<6;j++)
    
    printf(" %.1f",murphy[i][j]);
    printf("\n");
    
    
    }
    
    //line spaces
    printf("\n");
    printf("\n");
    //test print
    printf("%.1lf",max);
    //line spaces
    printf("\n");
    printf("\n");
    //test print
    printf("%.1lf",min);
    
    
    return 0;
    
    
    
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > printf("Enter Degree of Difficulty For Dive %d Diver 1 (1-4) >>",counter_2);
    > scanf("%lf",&murphy[i][j]);
    What is the value of j at this point?
    Bet it isn't zero like you expect

    Try
    scanf("%lf",&murphy[i][0]);


    > for (i=0;i<6;i++)
    ...
    > for(i=0;i<3;i++)
    Be consistent about whether i is subscripting the rows or columns of the 2D array
    Better yet, rename i,j to be row,col

    Your approach to indentation needs work as well. If it looks OK in your editor, then turn off TABS because they invariably result in a mess when you copy/paste code to the board.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jan 2004
    Posts
    3
    Huge Thx Salem...

    I'm getting in the missing data now, but It still needs a bit of work on min and max...its my loops that are causing it...as you spotted..so I'm off to fix that

    Your help is gratefully appreciated

    Praxis

  4. #4
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164
    Originally posted by praxis
    Huge Thx Salem...

    I'm getting in the missing data now, but It still needs a bit of work on min and max...its my loops that are causing it...as you spotted..so I'm off to fix that

    Your help is gratefully appreciated

    Praxis
    Your min test needs a major overhaul. All you are going to get is the last min value that's less than max.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  5. #5
    Registered User
    Join Date
    Jan 2004
    Posts
    3
    Yep the Min and Max were wrong...but its fixed now...
    That's the fun part ..fixing your misteeks
    Thx

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 05-29-2009, 07:25 PM
  2. Replies: 6
    Last Post: 11-09-2006, 03:28 AM
  3. [question]Analyzing data in a two-dimensional array
    By burbose in forum C Programming
    Replies: 2
    Last Post: 06-13-2005, 07:31 AM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM