Thread: C programming

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    6

    C programming

    Code:
    Hello.
    I need some help, please. 
    Thank you.
    
    1. To mirror the numbers in a two-dimensional array. For instance for a number, if the number is : 2345, and the number in mirror is 5432.
    
    #include<stdio.h>
    #include<math.h>
    #define matrix [20][20]
    #define matrix1 [20][20]
    void main()
    {
       int number_lines, number_columns, i, j, ogl, nr;
       printf("\n Give the number of lines : ");
       scanf("%ld", &number_lines);
       printf("\n Give the number of columns : ");
       scanf("%ld", &number_columns);
       printf("\n Give the elements of matrix : ");
       printf("\n");
       for(i=1; i<=number_lines; i++) 
          {
            for(j=1; j<=number_columns; j++)  
                {
                   printf("\n Matrix [%d][%d] = ", i, j);
                   scanf("%d", &nr);
                   matrix1[i][j] = matrix[i][j];
                }
          }
       for(i=1; i<=number_lines; i++)  
           {
              for(j=1; j<=number_columns; j++)  
                  {
                     ogl = 0;
                     while(matrix[i][j] != 0)
                        {
                            ogl = ogl * 10 + matrix[i][j] % 10;
                            matrix[i][j] = matrix[i][j] / 10;
                        }
            printf("\n The mirror of matrix[%d][%d] --> %d is %d\n", i, j, matrix1[i][j], ogl);
            }
        }
    }
    
    
    2. To carry out a program to display prime numbers from a given range. 
    
    #include<stdio.h> 
    void main() 
    { 
        int a, b, prime, d, i; 
        printf("\n Give a = "); 
        scanf("%d", &a); 
        printf("\n Give b = "); 
        scanf("%d", &b); 
        if(b<=a) 
        printf("\n b must be greather than a.\n"); 
        prime=1; d=2; 
          for(i=a; i<=b; i++) 
              { 
                  while(d<=i/2 && prime) 
                        { 
                            if(i%d==0) 
                            prime=0; 
                            d++; 
                        } 
                 if(prime) 
                   printf(" %d", i); 
                 else 
                   printf("\n No prime in the interval [%d %d].\n", a, b); 
             } 
       }

  2. #2
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    693
    Remember that void main() is not accepted on this forum lol make it int main() and return 0 at the bottom another thing is remember that your arrays start at 0 you have your for loops starting at 1 so you will be skipping the actual first column and row when inputting

  3. #3
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Good effort with the code tags, but you missed the mark a bit. Use one set of code tags for each problem/program. Don't put your assignment description, questions, etc in code tags.

    It's int main(void) and return 0; at the end. Read this link.


    The following are for your mirror program:
    • You don't declare a 2-d array (or any array) with #define. Read our array tutorial and Google for some more. Crack open that textbook too.
    • Read the scanf documentation. %ld is for long integers. You want %d for regular ints.
    • Arrays in C go from 0 to size-1, so for a 20x20 matrix, the highest indexes are [19][19]. You should use for (i = 0; i < size; i++) for your array processing loops.
    • You scanf into nr, but don't do anything with nr. You then copy from matrix (which is uninitialized and full of garbage) into matrix1.
    • After that, you reverse the garbage numbers in matrix, and print out the unreversed matrix1.


    This is for your prime program:
    • You need to set prime to 1 every time that while loop runs. Same thing with setting d = 2.
    • You need a separate variable to track if there were any primes whatsoever in the interval.
    • If that separate variable is true, print "No primes..." only once, after your for loop is done.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. About Device Driver Programming And Socket Programming
    By pritesh in forum Linux Programming
    Replies: 6
    Last Post: 01-23-2010, 03:46 AM
  2. Replies: 1
    Last Post: 08-19-2007, 03:55 AM
  3. small programming job VCPP / Object Oriented Programming
    By calgonite in forum Projects and Job Recruitment
    Replies: 10
    Last Post: 01-04-2006, 11:48 PM
  4. Total newb to programming here... Question about the many programming languages. Ty!
    By tsubotakid1 in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 10-05-2003, 10:32 AM

Tags for this Thread