Program Analysis

This is a discussion on Program Analysis within the C++ Programming forums, part of the General Programming Boards category; I'm having to do a bit of program analysis for one of my programs that is due for class tommorrow, ...

  1. #1
    Unregistered
    Guest

    Question Program Analysis

    I'm having to do a bit of program analysis for one of my programs that is due for class tommorrow, and I'm a bit stuck on a very simple part for some people.

    Ok I have to analyze three sections of my program. The function bubble sort, linear search, and binary search. Lets just focus on bubble sort since I should be able to apply the same concepts to the rest of the functions. Here's a snipit of code from the bubble sort function.

    Bubble Sort:
    void sortArray(int a[], int size)
    {
    int tmp;
    for (int i = 0; i < size - 1; i = i + 1)
    {
    for (int j = 0; j < size - 1; j = j + 1)
    if (a[j + 1] < a[j])
    {
    tmp = a[j];
    a[j] = a[j + 1];
    a[j + 1] = tmp;
    }
    }
    }

    I'm not real sure, but I think either the inner for loop or the if (a[j + 1] < a[j]) gets called the most. In which case I want to keep a count of how many times the one that's called the most gets executed. The only problem is I'm not real sure how to keep a count of how many times it's exectued. Can anyone enlighten me?

  2. #2
    zen
    zen is offline
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    You could use a variable(s) that is incremented everytime part of your code is executed.
    zen

  3. #3
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    Dear oh dear..... for loops are COUNTED loops. You can work this out in your head. For every time the outer loop iterates the inner loop iterates size-1 times.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  4. #4
    Unregistered
    Guest
    Originally posted by zen
    You could use a variable(s) that is incremented everytime part of your code is executed.
    Ok, Lets say I want to increament the variable everytime if(a[j + 1] < a[j]) statement is executed. How would I do that in code. That's my real problem.

    Ok Stonded_Coder,

    So basically I need to add a piece of code to my statement as such:

    for (int i = 0; i < size -1; i = i + 1)
    {
    countBS = countBS + j;
    for (int j = 0; j < size - 1; j = j + 1)
    {
    blah blah
    }

    since j is going to end of being size - 1. Correct?
    btw: countBS stands for count_Bubble_Sort
    I'm not sure, but I might need to declare j outside of the inner loop in order to do that.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Issue with program that's calling a function and has a loop
    By tigerfansince84 in forum C++ Programming
    Replies: 9
    Last Post: 11-12-2008, 12:38 PM
  2. Need help with a program, theres something in it for you
    By engstudent363 in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 12:41 PM
  3. Replies: 4
    Last Post: 02-21-2008, 09:39 AM
  4. Replies: 3
    Last Post: 03-04-2005, 01:46 PM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM

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