0% found this document useful (0 votes)
4 views

Basic Programming Laboratory

The document details the process of manually working through code without execution known as a dry run. It provides examples of dry running code that swaps two integers using a temporary variable and using addition and subtraction. The dry run traces the values of variables step-by-step to identify logic errors.

Uploaded by

schoolisbest2019
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Basic Programming Laboratory

The document details the process of manually working through code without execution known as a dry run. It provides examples of dry running code that swaps two integers using a temporary variable and using addition and subtraction. The dry run traces the values of variables step-by-step to identify logic errors.

Uploaded by

schoolisbest2019
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

Dry run

Basic Programming (CS 1000)

Sumanta Pyne

Assistant Professor
Computer Science and Engineering Department
National Institute of Technology Rourkela
[email protected]

August 25, 2023

Sumanta Pyne (NITRKL) Autumn 2021, Dry run August 25, 2023 1 / 16
Dry run

Process of manually working through code to trace value of variables


There is no software involved in this process
Characteristics of a dry run
carried out during design, implementation, testing or maintenance
used to identify logic errors
not find compile time (syntax, semantics) errors and runtime (execution) errors

Sumanta Pyne (NITRKL) Autumn 2021, Dry run August 25, 2023 2 / 16
Swap using an extra variable

/* swap temp.c written by Sumanta Pyne on 24/12/2021.


Swaps two integers with help a third variable.*/
#include<stdio.h> // header file for library function printf
int main() // main function begins
{
int a,b,temp; // swap variables a and b, temporary variable temp
printf(”Enter first value:”); //Entry of a
scantf(”%d ”,&a); //Read a
printf(”First value: %d\n”,a ); // Print a
printf(”Enter second value:”); //Entry of b
scantf(”%d ”,&b); //Read b
printf(”Second value: %d\n”,b); // Print b
printf(”Before swap: First value = %d, Second value = %d\n”,a,b); //Print input
temp=a; // store a in temp
a=b; // store b in a
b=temp; // store temp in a
printf(”After swap: First value = %d, Second value = %d\n”,a,b); //Print output
return 0; // returns integer value 0
} // end of main function

Sumanta Pyne (NITRKL) Autumn 2021, Dry run August 25, 2023 3 / 16
Start, read and display input

#include<stdio.h>
int main()
{ a= ? 9
int a,b,temp;
printf(”Enter first value:”); b= ? 7
scantf(”%d ”,&a);
printf(”First value: %d\n”,a ); temp= ?
printf(”Enter second value:”);
scantf(”%d ”,&b);
printf(”Second value: %d\n”,b);
printf(”Before swap: First value = %d, Second value = %d\n”,a,b);
temp=a;
a=b;
b=temp;
printf(”After swap: First value = %d, Second value = %d\n”,a,b);
return 0;
}

Sumanta Pyne (NITRKL) Autumn 2021, Dry run August 25, 2023 4 / 16
temp ← a; store a in temp

#include<stdio.h>
int main()
{ a= ? 9
int a,b,temp;
printf(”Enter first value:”); b= ? 7
scantf(”%d ”,&a);
printf(”First value: %d\n”,a ); temp= ? 9
printf(”Enter second value:”);
scantf(”%d ”,&b);
printf(”Second value: %d\n”,b);
printf(”Before swap: First value = %d, Second value = %d\n”,a,b);
temp=a;
a=b;
b=temp;
printf(”After swap: First value = %d, Second value = %d\n”,a,b);
return 0;
}

Sumanta Pyne (NITRKL) Autumn 2021, Dry run August 25, 2023 5 / 16
a ← b; store b in a

#include<stdio.h>
int main()
{ a= ? 9 7
int a,b,temp;
printf(”Enter first value:”); b= ? 7
scantf(”%d ”,&a);
printf(”First value: %d\n”,a ); temp= ? 9
printf(”Enter second value:”);
scantf(”%d ”,&b);
printf(”Second value: %d\n”,b);
printf(”Before swap: First value = %d, Second value = %d\n”,a,b);
temp=a;
a=b;
b=temp;
printf(”After swap: First value = %d, Second value = %d\n”,a,b);
return 0;
}

Sumanta Pyne (NITRKL) Autumn 2021, Dry run August 25, 2023 6 / 16
b ← temp; store temp in b

#include<stdio.h>
int main()
{ a= ? 9 7
int a,b,temp;
printf(”Enter first value:”); b= ? 7 9
scantf(”%d ”,&a);
printf(”First value: %d\n”,a ); temp= ? 9
printf(”Enter second value:”);
scantf(”%d ”,&b);
printf(”Second value: %d\n”,b);
printf(”Before swap: First value = %d, Second value = %d\n”,a,b);
temp=a;
a=b;
b=temp;
printf(”After swap: First value = %d, Second value = %d\n”,a,b);
return 0;
}

Sumanta Pyne (NITRKL) Autumn 2021, Dry run August 25, 2023 7 / 16
Display output and stop

#include<stdio.h>
int main()
{
int a,b,temp;
printf(”Enter first value:”);
scantf(”%d ”,&a);
printf(”First value: %d\n”,a );
printf(”Enter second value:”);
scantf(”%d ”,&b);
printf(”Second value: %d\n”,b);
printf(”Before swap: First value = %d, Second value = %d\n”,a,b);
temp=a;
a=b;
b=temp;
printf(”After swap: First value = %d, Second value = %d\n”,a,b);
return 0;
}

Sumanta Pyne (NITRKL) Autumn 2021, Dry run August 25, 2023 8 / 16
Swap using addition-subtraction

/* swap addsub.c written by Sumanta Pyne on 24/12/2021.


Swaps two integers with add-subtract.*/
#include<stdio.h> // header file for library function printf
int main() // main function begins
{
int a,b; // swap variables a and b
printf(”Enter first value:”); //Entry of a
scantf(”%d ”,&a); //Read a
printf(”First value: %d\n”,a ); // Print a
printf(”Enter second value:”); //Entry of b
scantf(”%d ”,&b); //Read b
printf(”Second value: %d\n”,b); // Print b
printf(”Before swap: First value = %d, Second value = %d\n”,a,b); //Print input
a=a+b; // store a + b in a
b=a-b; // store a - b in b
a=a-b; // store a-b in a
printf(”After swap: First value = %d, Second value = %d\n”,a,b); //Print output
return 0; // returns integer value 0
} // end of main function

Sumanta Pyne (NITRKL) Autumn 2021, Dry run August 25, 2023 9 / 16
Start, read and display input

#include<stdio.h>
int main()
{ a= ? 9
int a,b;
printf(”Enter first value:”); b= ? 7
scantf(”%d ”,&a);
printf(”First value: %d\n”,a );
printf(”Enter second value:”);
scantf(”%d ”,&b);
printf(”Second value: %d\n”,b);
printf(”Before swap: First value = %d, Second value = %d\n”,a,b);
a=a+b;
b=a-b;
a=a-b;
printf(”After swap: First value = %d, Second value = %d\n”,a,b);
return 0;
}

Sumanta Pyne (NITRKL) Autumn 2021, Dry run August 25, 2023 10 / 16
a ← a + b; store a + b in a

#include<stdio.h>
int main()
{ a= ? 9 16
int a,b;
printf(”Enter first value:”); b= ? 7
scantf(”%d ”,&a);
printf(”First value: %d\n”,a );
printf(”Enter second value:”);
scantf(”%d ”,&b);
printf(”Second value: %d\n”,b);
printf(”Before swap: First value = %d, Second value = %d\n”,a,b);
a=a+b;
b=a-b;
a=a-b;
printf(”After swap: First value = %d, Second value = %d\n”,a,b);
return 0;
}

Sumanta Pyne (NITRKL) Autumn 2021, Dry run August 25, 2023 11 / 16
b ← a - b; store a - b in b

#include<stdio.h>
int main()
{ a= ? 9 16
int a,b;
printf(”Enter first value:”); b= ? 7 9
scantf(”%d ”,&a);
printf(”First value: %d\n”,a );
printf(”Enter second value:”);
scantf(”%d ”,&b);
printf(”Second value: %d\n”,b);
printf(”Before swap: First value = %d, Second value = %d\n”,a,b);
a=a+b;
b=a-b;
a=a-b;
printf(”After swap: First value = %d, Second value = %d\n”,a,b);
return 0;
}

Sumanta Pyne (NITRKL) Autumn 2021, Dry run August 25, 2023 12 / 16
a ← a - b; store a - b in a

#include<stdio.h>
int main()
{ a= ? 9 16 7
int a,b,temp;
printf(”Enter first value:”); b= ? 7 9
scantf(”%d ”,&a);
printf(”First value: %d\n”,a );
printf(”Enter second value:”);
scantf(”%d ”,&b);
printf(”Second value: %d\n”,b);
printf(”Before swap: First value = %d, Second value = %d\n”,a,b);
a=a+b;
b=a-b;
a=a-b;
printf(”After swap: First value = %d, Second value = %d\n”,a,b);
return 0;
}

Sumanta Pyne (NITRKL) Autumn 2021, Dry run August 25, 2023 13 / 16
Display output and stop

#include<stdio.h>
int main()
{
int a,b;
printf(”Enter first value:”);
scantf(”%d ”,&a);
printf(”First value: %d\n”,a );
printf(”Enter second value:”);
scantf(”%d ”,&b);
printf(”Second value: %d\n”,b);
printf(”Before swap: First value = %d, Second value = %d\n”,a,b);
a=a+b;
b=a-b;
a=a-b;
printf(”After swap: First value = %d, Second value = %d\n”,a,b);
return 0;
}

Sumanta Pyne (NITRKL) Autumn 2021, Dry run August 25, 2023 14 / 16
Swap using multiplication-division

Write a C program to swap two integer values using multiply and divide operations.
Perform a dry run of your code considering the given input is 9 and 7.

Sumanta Pyne (NITRKL) Autumn 2021, Dry run August 25, 2023 15 / 16
Thank you

Sumanta Pyne (NITRKL) Autumn 2021, Dry run August 25, 2023 16 / 16

You might also like