2 dimensional array

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • nithu m
    New Member
    • Nov 2011
    • 5

    2 dimensional array

    IN THIS PROGRAM HOW TAT pivot IS COMPARED WITH FREQUENCY [0] [k] ?because frequency array doesnot have any values then how is it possible to compare ?
    can anyone pls explain this program to me ?

    #include<stdio. h>
    #include<conio. h>
    main(){
    int MAX =20;

    int arr[20] = {1,2,30,4,1,2,6 ,8,5,0,9,8,3,5, 6,7,2,6,1,1};
    int frequency[2][20];
    int counter = 0;
    int flag = 0;
    int i =0,j=0,k=0;
    int pivot;

    for (i=0;i<MAX;i++)
    {
    pivot = arr[i];
    for (k=0;k<j;k++)
    {
    if (pivot == frequency[0][k])
    {
    flag = 1;
    break;
    }
    }

    if (flag == 0)
    {
    for(k=0;k<MAX;k ++)
    {
    if(pivot ==arr[k])
    counter++;
    }

    frequency[0][j] = pivot;
    frequency[1][j] = counter;
    counter = 0;
    j++;
    }
    flag= 0;
    }

    for(i=0;i<j;i++ )
    {
    printf("Element : %d, Frequency : %d \n",frequency[0][i],frequency[1][i]);
    }



    getch();
    }
  • johny10151981
    Top Contributor
    • Jan 2010
    • 1059

    #2
    It does not check without setting, take a look at the beginning j value is zero and so the loop with k never get started.

    so, flag remain zero.

    so it will enter second loop that start from k to MAX, after finishing loop it initialize frequency for the first time, and then j get increased reached to 1 and so on

    Comment

    • nithu m
      New Member
      • Nov 2011
      • 5

      #3
      thank you mr.johny i understood tis program now

      Comment

      • johny10151981
        Top Contributor
        • Jan 2010
        • 1059

        #4
        Please learn how to debug. It will give you faster answer than forum

        Comment

        • nithu m
          New Member
          • Nov 2011
          • 5

          #5
          im using dev c++ software so it is bit difficult to debug a line or loop seperatly

          Comment

          • whodgson
            Contributor
            • Jan 2007
            • 542

            #6
            "im using dev c++ software so it is bit difficult to debug a line or loop seperatly"
            rubbish!

            Comment

            Working...