validation in c

This is a discussion on validation in c within the C Programming forums, part of the General Programming Boards category; can anybody help me how to perform unit testing of the below c program i)While getting integer input it must ...

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    2

    Unhappy validation in c

    can anybody help me how to perform unit testing of the below c program
    i)While getting integer input it must get only integers
    ii)While getting string input it must get only string

    Matrix multiplication:
    Code:
    #include<stdio.h>
    #include<conio.h>
    int main()
    {
    int choice,m1[10][10],i,j,k,m2[10][10],mult[10][10],r1,c1,r2,c2;
    do
    {
    printf("Enter number of rows and columns of first matrix \n");
    scanf("%d%d",&r1,&c1);
    printf("Enter number of rows and columns of second matrix \n");
    scanf("%d%d",&r2,&c2);
    if(r2==c1)
    {
    printf("Enter the elements 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 the elements 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 not posssible");
    }
    printf("\n\nPress 1/0 to continue or exit: ");
    scanf("%d",&choice);
    }while(choice==1);
    getch();
    return 0;
    }


  2. #2
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,304
    Well there are no strings declared or being read in in that program so ii) should be pretty easy!
    For i), check the return value of scanf. Also check the FAQs on this site and they can probably help you with the next thing you'll run into.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  3. #3
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    One word ... "indentation".

    You're code is almost unreadable.

  4. #4
    Registered User
    Join Date
    May 2009
    Posts
    2,572
    From Unit testing - Wikipedia, the free encyclopedia
    In computer programming, unit testing is a method by which individual units of source code are tested to determine if they are fit for use. A unit is the smallest testable part of an application. In procedural programming a unit could be an entire module but is more commonly an individual function or procedure.
    I see no functions in your program.

    Tim S.

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,535
    The lack of indentation seems to be a good indication of copy/paste code.

    In particular, it seems to be just a hatchet job of this from 2006 -> Multiplication of 2 Matrix in C

    Almost certain to be another 1-post driveby.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. c++ validation
    By rahulsk1947 in forum C++ Programming
    Replies: 4
    Last Post: 06-08-2009, 01:53 PM
  2. validation
    By chodmama in forum C Programming
    Replies: 3
    Last Post: 02-20-2006, 12:05 AM
  3. validation
    By blanny in forum C Programming
    Replies: 9
    Last Post: 03-05-2004, 06:43 PM
  4. need help in validation
    By dholman in forum C Programming
    Replies: 2
    Last Post: 01-08-2004, 08:27 AM
  5. Help with validation
    By boontune in forum C++ Programming
    Replies: 8
    Last Post: 01-10-2003, 08:44 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21