Thread: Program

    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    1
    Rep Power
    0

    Program


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

    typedef char string [128];
    main(){
    string memory [16] = {0};
    string search;
    int choice;
    int i;
    int size = 0;
    while(1){
    system("CLS");
    int found = 0;
    printf("Command prompt\n");
    printf("1.) Enter string in database\n");
    printf("2.) Remove a string from database\n");
    printf("3.) View strings in database\n");
    printf("4.) Search a string\n");
    printf("5.) Exit.\n");
    printf("Choose a number that you want to do.\n");
    scanf("%d", &choice);

    switch(choice){

    case 1:
    printf("Entry #%d: ",size + 1);
    while(getchar()!='\n');
    gets(memory[size]);
    size++;
    break;
    case 2:
    printf("Enter entry number: ");
    scanf("%d",&choice);
    choice--;
    for (i=choice; i<size ; i++)
    strcpy(memory[i],memory[i+1]);
    size--;
    break;

    case 3:
    for (i = 0; i < size; i++)
    printf("Entry #%d: %s\n",i+1,memory[i]);
    break;
    case 4:
    printf("Search: ");
    while(getchar()!='\n');
    gets(search);
    // linear search
    i = 0;
    for (; i< size; i++)
    if (strcmp(search,memory[i])==0){
    found = 1;
    break;
    }

    if (found)
    printf("Data is found in number#%d: %s", i+1,search);
    else
    printf("Data not found.");

    break;

    case 5:
    exit(1);
    break;
    }
    printf("\n");
    system("pause");
    }
    }

    polynomial

    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>



    main()
    {
    system("cls");
    int a[10];
    int order, constant, i, power;
    float sum,x;
    system ("cls");
    printf("Enter the order of the polynomial\n");
    scanf("%d", &order);

    printf("Enter constant for x\n");
    scanf("%d", &constant);


    printf("Enter %d coefficients\n", order+1);
    for (i=0; i<= order; i++)
    {
    scanf("%d", &a[i]);
    }
    sum = a[0];
    for (i=1; i<= order; i++)
    {
    sum = sum*constant + a[i];
    }

    power = order;

    printf("Given polynomial is:\n");
    for (i=0;i<=order;i++)
    {
    if (power < 0)
    {
    break;
    }
    if (a[i] > 0)
    printf("+");
    else if (a[i] < 0)
    printf("-");
    else
    printf(" ");
    printf("%dx^%d", abs(a[i]), power--);
    }
    printf("\nSum of the polynomial = %6.2f\n\n", sum);

    system("pause");

    }
    mult

    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>

    main()
    {
    int m1[10][10],i,j,k,m2[10][10],mult[10][10],r1,c1,r2,c2;
    printf("Enter number of rows and columns of first matrix (less than 10)\n");
    scanf("%d%d",&r1,&c1);
    printf("Enter number of rows and columns of second matrix (less than 10)\n");
    scanf("%d%d",&r2,&c2);
    if(r2==c1)
    {
    printf("Enter rows and columns of First matrix \n");
    printf("Row wise\n");
    for(i=0;i<r1;i++)
    for(j=0;j<c1;j++)
    scanf("%d",&m1[i][j]);
    printf("First Matrix is :\n");
    for(i=0;i<r1;i++)
    {
    for(j=0;j<c1;j++)
    printf("%d\t",m1[i][j]);
    printf("\n");
    }
    printf("Enter rows and columns of Second matrix \n");
    printf("Row wise\n");
    for(i=0;i<r2;i++)
    for(j=0;j<c2;j++)
    scanf("%d",&m2[i][j]);
    printf("Second Matrix is:\n");
    for(i=0;i<r2;i++)
    {
    for(j=0;j<c2;j++)
    printf("%d\t",m2[i][j]);
    printf("\n");
    }
    printf("Multiplication of the Matrices:\n");
    for(i=0;i<r1;i++)
    {
    for(j=0;j<c2;j++)
    {
    mult[i][j]=0;
    for(k=0;k<r1;k++)
    mult[i][j]+=m1[i][k]*m2[k][j];
    printf("%d\t",mult[i][j]);
    }
    printf("\n");
    }
    }
    else
    {
    printf("Matrix multiplication cannot be done");
    }
    system("pause");

    }
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    71
    Rep Power
    2
    Not sure why you posted this but could you surround your code with code tags and possibly try formatting it?
  4. #3
  5. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,897
    Rep Power
    481

    Agree. I often use int i; myself.


    Click here links to a website targeted for working code:
    http://code.activestate.com/recipes/langs/c/


    I tried your polynomial program.

    Zeroth: the program doesn't port well to my operating system.

    First off, it doesn't tell me whether to order the coefficients from constant toward higher power terms or the other way around.

    Secondly, I used 0 for some of the coefficients. The polynomial displays incorrectly.
    Code:
    $ ./polynomial
    sh: 1: cls: not found
    Enter the order of the polynomial
    3
    Enter constant for x
    0
    Enter 4 coefficients
    1 0 0 1
    Given polynomial is:
    +1x^3 0x^2 0x^1+1x^0
    Sum of the polynomial =   1.00
    (I think there was also a system("pause"); statement which I deleted before compilation. Likewise, it wouldn't have worked on my operating system.)




    (also, I'm sure that your program would break if I entered a 40th order polynomial.)
    Last edited by b49P23TIvg; October 29th, 2012 at 09:23 AM. Reason: fix [/url]
    [code]Code tags[/code] are essential for python code and Makefiles!

IMN logo majestic logo threadwatch logo seochat tools logo