1. DLP Flash Christmas Competition + Writing Marathon 2024!

    Competition topic: Magical New Year!

    Marathon goal? Crank out words!

    Check the marathon thread or competition thread for details.

    Dismiss Notice
  2. Hi there, Guest

    Only registered users can really experience what DLP has to offer. Many forums are only accessible if you have an account. Why don't you register?
    Dismiss Notice
  3. Introducing for your Perusing Pleasure

    New Thread Thursday
    +
    Shit Post Sunday

    READ ME
    Dismiss Notice

Programming !! :D

Discussion in 'Tech Support' started by Skykes, Dec 7, 2010.

  1. Johnny Farrar

    Johnny Farrar High Inquisitor

    Joined:
    Mar 14, 2009
    Messages:
    521
    Location:
    In front of a Computer.
    I started with C# followed it with C, PHP and Python. Later learned C++ and then Java.

    Nowadays, most of my programming is done in Java and C++, with Python for whatever scripts that I need to write for Linux occasionally.
     
  2. Alindrome

    Alindrome A bigger, darker mark DLP Supporter Retired Staff

    Joined:
    Apr 9, 2009
    Messages:
    2,771
    Gender:
    Female
    Location:
    England
    Ah, I didn't mean it as a language in itself, but I do consider data structures to be in the same sort of boat. ;)
     
  3. Skykes

    Skykes Minister of Magic DLP Supporter

    Joined:
    May 14, 2006
    Messages:
    1,353
    Location:
    Ireland
    Hey again,

    After a serious ammount of reading, many failed attempts, I finally managed it. Well except for one part.

    I can't get the % sparsity to work(I don't really understand what have to do).

    If any of you have some spare time to look over this code it would be cool. It does compile*(with some warnings), and it works.
     
    Last edited: Dec 17, 2010
  4. Perspicacity

    Perspicacity Destroyer of Worlds ~ Prestige ~ DLP Supporter

    Joined:
    Nov 27, 2007
    Messages:
    1,022
    Location:
    Where idiots are not legally permitted to vote
    High Score:
    3,994
    It looks like you've done quite a lot of self-study, which is heartening to see.

    conio.h isn't part of the standard C libraries. I'm on a BSD machine, so I can't comment on any warnings with your use of these methods/symbols. The following will fix one warning, however. main returns an int:

    Code:
    int main( int argc, char *argv[] ) { 
     
      /* code */ 
    
      return 0;
    }
    
    As a programmer, you should understand every warning and eliminate all of them (or as many as you can, anyway). Warnings are an indication that the compiler got confused, which is never a good thing since compilers are dumb as dogshit.

    For percent sparsity, I think you just need to loop through the matrices and count how many elements are zero. Something like:

    Code:
    { 
      int nzero_A=0, nzero_B=0; 
      int percent_sparsity_A, percent_sparsity_B; 
    
      for ( i=0; i<5; ++i ) 
        for ( j=0; j<5; ++j )
          if ( A[i][j]==0 ) ++nzero_A;
          if ( B[i][j]==0 ) ++nzero_B;
        }
      percent_sparsity_A = nzero_A*4;  /* each element is 4% of total # elem. */  
      percent_sparsity_B = nzero_B*4;
     
      /* then do stuff like print it out or something */ 
    
    }
    
     
  5. KrzaQ

    KrzaQ Denarii Host DLP Supporter

    Joined:
    May 9, 2008
    Messages:
    1,404
    Location:
    Poland
    It's because you don't have another if for option 4.
    Code:
        else if (option==4){
            int zeros = 0;
            for (i=0; i<25; i++) if(A[i/5][i%5] == 0) zeros++;
            printf("sparsity of matrix 1: %i\n",zeros*4);
            zeros = 0;
            for (i=0; i<25; i++) if(B[i/5][i%5] == 0) zeros++;
            printf("sparsity of matrix 2: %i\n",zeros*4);
        }
    I didn't bother to check if the rest of your code is correct, but it seems so (more or less).

    I still think you should use my awesome solution, though.
     
    Last edited: Dec 15, 2010
  6. Skykes

    Skykes Minister of Magic DLP Supporter

    Joined:
    May 14, 2006
    Messages:
    1,353
    Location:
    Ireland
    Alright, figured it all out. Program works 100%. I'm just adding comments now and then bed.

    Thanks for the kick in the arse guys.

    edit: So I was susposed to do a design before I actually started. Not really sure what he wants. Does this look alright or is more detail required?

    Code:
    Program Design for Assignment 1: Matrices
    Level 1(Overview)
    Input menu number 
    While choice is not = 7 run loop
    If choice = 1 then input data for Matrix A
    	Prompts user to fill matrix A
    		As long as column is less than 5, increment column
    			As long as row is less than 5, increment row
    				Displays current row and current number to user 
    
    
    Also, for those interested, I updated the code.

    Code:
    #include <stdio.h>/*Standard input/output*/
    #include <conio.h>/*Console input/output*/
    
    
    void main()
    {
        int A[5][5], B[5][5], z, x, y;/*Declaring integers*/
        int choice;
        int C[5][5]={{0}};
        int countX;
        int countY;
        float sparsityX;
        float sparsityY;
        
        printf("Please follow the prompt bellow and\nselect 1 and 2 first\n");/*Shows menu to user when program is executed*/
        printf("1: Input data into matrix 1 \n");
        printf("2: Input data into matrix 2 \n");
        printf("3: Display the matrices in horizontal \n");
        printf("4: Determine sparsity for matrix 1 and matrix 2\n");
        printf("5: Addition of matrix 1 and matrix 2\n");
        printf("6: Multiplication of matrix 1 and matrix 2\n");
        printf("7: Exit Program\n");
        scanf("%d", &choice);
        fflush(stdin);
       
        while (choice!=7)/* Run nested loop as long as option is not equal to 7*/
        {
            
        if ( choice == 1 )/* Scans for integers to fill matrix A*/
        {
            puts("1: Fill matrix A");
            for (z=0; z<5; z++)
                {
                    for (x=0; x<5; x++)
                        {
                            printf("row no: %3d column no: %3d --> ", z+1, x+1);
                            scanf("%d", &A[z][x]);
                            fflush(stdin);
                        }
                }
        }
        
        else if ( choice == 2 )/* Scans for integers to fill matrix B*/
        {
            puts("2: Fill matrix B");
            for (z=0; z<5; z++)
                {
                    for (x=0; x<5; x++)
                        {
                            printf("row no: %3d column no: %3d --> ",z+1, x+1);
                            scanf("%d", &B[z][x]);
                            fflush(stdin);
                        }
                }
        }
        
        
        else if ( choice == 3)/*Displays the matrices to user*/
        {
            printf("Matrix A:  \t  Matrix B: \n");
            for (z=0; z<5; z++)
                {
                    for (x=0; x<5; x++)
                        {
                            printf("%4d", A[z][x]);
                        }
                    printf("            ");
                    for (x=0; x<5; x++)
                        {
                            printf("%4d", B[z][x]);
                        }
                    printf("\n");
                }
        }
        
        else if ( choice == 4)/*Displays the % sparsity for first matrix, does newline and displays second matrix % sparsity */
        {
            countX=0;
            for(z=0; z<5; z++)
                {
                    for(x=0; x<5; x++)
                        {
                            if(A[z][x]==0)
                                countX++;
                        }
                }
            sparsityX= (float) countX/25*100;
            countY=0;
            for(z=0; z<5; z++)
                {
                    for(x=0; x<5; x++)
                        {
                            if(B[z][x]==0)
                                countY++;
                        }
                }
            sparsityY= (float) countY/25*100;
            
            printf("\nSparsity for matrix 1 is %.2f percent\nSparsity for matrix 2 is %.2f percent\n",sparsityX,sparsityY);
        }
                
        
        
        else if ( choice == 5 )/*Adds the 2 matrices together */
        {
            printf("You picked an option no: 5\n");
            for (z=0; z<5; z++)
            {
                for (x=0; x<5; x++)
                {
                    C[z][x]=A[z][x]+B[z][x];/*Formula for the addition*/
                    printf("%4d", C[z][x]);
                }
                printf("\n");
                fflush(stdin);
            }
        }
        
    else if ( choice == 6 )/*Multiplies the 2 matrices*/
        {
            printf("You picked an option no: 6\n");
            for (z=0; z<5; z++)
            {
                for (x=0; x<5; x++)
                {
                    C[z][x]=0;
                    for (y=0; y<5; y++)
                    {
                        C[z][x]=C[z][x]+A[z][y]*B[y][x];/*Formula for the multiplication */
                    }
                }
            }
            for (z=0; z<5; z++)
            {
                for (x=0; x<5; x++)
                {
                    printf("%5d", C[z][x]);
                        
                }
                printf("\n");
            }
        }
    
           
        
        else if ( choice == 7 )/*Break, then end program */
            
        {
            printf("\n\nEnd program\n\n");
            break;
        }
        
        
        printf(" Select option \n");
        printf("1: Input data into matrix 1 \n");
        printf("2: Input data into matrix 2 \n");
        printf("3: Display the matrices in horizontal \n");
        printf("4: Determine sparsity for matrix 1 and matrix 2\n");
        printf("5: Addition of matrix 1 and matrix 2\n");
        printf("6: Multiplication of matrix 1 and matrix 2\n");
        printf("7: Exit Program\n");
        scanf("%d", &choice);
        fflush(stdin);
    }
    
        getch();
    }
    
    
     
    Last edited: Dec 17, 2010
Loading...