Click here to Skip to main content
15,567,786 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Program-1:

C++
for (i = 0; i < m, i + +){
    for (j = 0; j < n, j + +){
      if (a = b){
        arr[i][j] = 1;
      }
      else{
        arr[i][j] = 2;
      }
    }
}

for (i = 0; i < m, i + +){
    for (j = 0; j < n, j + +){
      arr[i] += arr[i][j];
    }
}

for (i = 0; i < m, i + +){
  if (a = b){
    p[i] = i
    q [i] = i
  }
}


Program-2:

C++
for (i = 0; i < m, i + +){
  sum = 0;
    for (j = 0; j < n, j + +){
      if (a = b){
        arr[i][j] = 1
        sum += 1;
      }
      else{
         arr[i][j] = 2
      }
    }
    if (c = d){
      arr[i] = sum;
      p[i] = i
      q [i] = i
    }   
}


What I have tried:

I tried to make a time complexity equation step by step for both program. and which one is better and how much? Could you please help on that?

for program-1:
m*n*c1 + m*n*c2 + m*c3

for program-2
m*n*c1 + m*c2
Posted
Updated 23-Sep-20 2:00am
v2
Comments

Not your question, but your programs are wrong:
C++
if (a = b){ // this store value of b in a

must be replaced by:
C++
if (a == b){ // this compare a and b for equality
 
Share this answer
 
Which do you think is faster? Look at the complexities you have calculated, and compare them.
O(1) has a fixed time: no matter how many data samples are used, it will always execute in the same time.
O(N) directly compares the number of data samples to the execution time in a linear fashion: if you increase N from 10 to 20, execution time will double.
O(2N) is also linear, if you increase N from 10 to 20, execution time will also double. The complexity is equivalent, but slower than O(2N)
O(N2) is exponential - a small increase in N results in a big increase in execution time.

Look at your two complexities and it's pretty obvious which is faster if you think about it.

This is your homework, not ours: your tutor is looking for you thoughts, not mine. So give it a try!
 
Share this answer
 
v2
Time complexity of both programs is O(m x n), so, why worry?
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900