C :: Method Or Process For Reducing Redundant Code In Nested Loops
Nov 8, 2014
Have an assignment due in a few weeks and I'm 99% happy with it My question is is there a method or process for reducing redundant code in nested loops. Ie my code compiles and runs as expected for a period of time and after a few goes it omits a part or prints an unexpected out ext so basically how to find when the redundancy occurs with out posting my code so I can learn for my self?
View 3 Replies
ADVERTISEMENT
Sep 4, 2014
ISSUE: How to avoid redundant code in c++ ?
Problem:Say I have a Base class called Car and it has 3 derived classes say Ford, Honda and Audi.
now the issue is all 3 derived classes have exactly same code but minor difference in calling member functions of their r respective engines( these engines have separate class) . example ford class calls code which is exactly same as other 2 classes but do call something like ford_engine-> start() ford_engine->clean() bla bla bla .
//ly honda calls honda_engine->start() honda_engine->clean();
//ly Audi calls audi_engine->start() audi->clean();
now here the issue is it has redundant code in all 3 places with minute difference. Hoow I can have code at one place most probably in Base class and all derive classes uses same code.
View 1 Replies
View Related
Apr 13, 2014
I have a class 'A' which is almost perfect for my needs. Class 'B' uses class 'A' I've now designed Class 'C' and Class 'D' and noticed that there is a good chunk of code in class 'B', 'C' and 'D' for using Class 'A' is duplicated. I've separated out this code in specific, standalone functions in each of the classes. Now I'm wondering where this code should go. At the moment, the functions are duplicated in the three calling classes (B, C and D). Placing the functions into class 'A' would break the single responsibility principle. Inheritance to add functionality would likely break both SRP and LSP. The one that seems that it may work is composition.
However, Is designing a complete class just for a few functions over kill?
Would it be valid for classes 'B', 'C' and 'D' to access both the new class 'E' (which would depend on A) and the old class 'A' (which would have to be the same instance as the instance in the new class 'E'), or should the new class 'E' provide sufficient functionality so that Classes B, C and D don't need to access Class A directly? It would seem that its then an incomplete interface of the original object with additional functionality (ie, incompatible) Or should I do it a completely different way?
View 4 Replies
View Related
Jun 1, 2013
What output would you expect from this program?" The output was not what I expected. I've psuedo-coded this out and I'm still missing something.
Code:
#include <stdio.h>
int main () {
int numbers[10] = { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
int i, j;
}
[code]....
The output: Code: 1 1 2 4 8 16 32 64 128 256 So when I look at this first loop I see that j = 0, which is less than 10, so then the program statement should commence, which is another for loop. So in this inner for loop I see that i = 0, which is not less than j, so this loop should terminate. Then the value of j increments by 1 and the first go around of the loop has completed.
Now I see that j = 1, so this is less than 10, and the inner for loop commences once again. This time though, i actually is less than j, so numbers[1] = numbers[1] + numbers [0], or numbers[1] = 0 + 1. Now the value of i is incremented by 1 and the first go around of this inner loop has completed. Then the value of j increments by 1 and another go around of that loop has completed.
So now j = 2, i = 1, and numbers[2] ( which is 0 ) = numbers[2] + numbers[1], or numbers[2] = 0 + 1. I was expecting the output to be an array full of 1's. However this is not the case..
View 6 Replies
View Related
Dec 7, 2013
Howi can made nested loops?
Code:
for (yax=0; yax<10; yax=yax+1) {
for (xax=0; xax<100; xax=xax+1) {
printf("%d
",yax);
}
}
way what i tired dont work. or maybe works but why this prints only zeros ?
View 8 Replies
View Related
Sep 27, 2014
So in class our teacher assigned us a program where we have to use nested for loops to creates triangles. How does the 2nd for loop print more than 1 star? since the for loop will only run the cout 1 time until it gets to the escape sequence, how does it print more than 1 star on a line? this is what is confusing me. I feel like if i can grasp the understanding of that and what the for loops are doing i can finish the rest of this program with ease
#include<iostream>
using namespace std;
int main()
[Code].....
View 1 Replies
View Related
Jul 11, 2013
Getting close but I think I am stuck on the second loop. The input you put in will be doubled (and it's not supposed to).
Code:
int main() {
int n, i, j, k;
printf("What would you like the height to be? (Positive odd integer)
");
scanf("%d", &n);
[Code] .....
View 3 Replies
View Related
Oct 6, 2013
The user will enter the number of '*'s on the 1st row (ntop) and then the number of rows forming the trapezoid (nrows). (using <iostream>, cout)
For instance, an inverted trapezoid with 7 '*"s in the 1st row and 3 rows forming the inverted trapezoid looks like:
1*******
2 *****
3 ***
(this pyramid is centered, in case it isnt when its posted). Also, each descending row has two less asteriks than the above row.
I am having trouble with the four loop displaying the number of "*" and " ". I know its a relationship with variables in the for loops, my output is just never doing what i want it to.
THis is the guideline for the for loop:
Use for loops to display the inverted trapezoid. Your outer for loop will iterate the total number of rows times. For each row use one nested for loop to display blanks (the 1st row contains no blanks) and another nested for loop to display the characters '*'.
Heres my for loops so far:
for (i = nrows; i >= 1; i--) {
for (j = 0; j >= nrows; j++) {
cout << " ";
} for (k=ntop; k >= 2; k--) {
cout << "*";
} }
The ouput is just blank as of now.
View 2 Replies
View Related
Sep 24, 2013
The output should be something like:
5
45
543
5432
54321
View 2 Replies
View Related
Mar 9, 2013
Write a program that prints a multiplication table using nested loops. In main ask the user for the smallest column number , the largest column number and the size of the increment. Ask the user for the same information for the row values.
In the example the column values entered are: 5, 15 and 2 and the row values 3, 6 and 1.
Given those numbers you would generate the following table.
Multiplication Table
| 5 7 9 11 13 15 ___|___________________________________ | 3 |
15 21 27 33 39 45 4 | 20 28 36 44 52 60 5 | 25 35 45 55 65 75 6 | 30 42 54 66 78 90
Print the 24 values with the grey background. The other numbers show the values to be multiplied.
Code:
#include<stdio.h>
main() {
int a,b,c,d,e,f;
int i,j,total;
printf("Please enter smallest column number: ");
scanf("%i",&a);
printf("
[Code] ....
Challenge:
As an added challenge try to print out the column
headings (5 7 9 11 13 15) and the row headings (3 4 5 6)
View 1 Replies
View Related
Feb 4, 2013
For each quarter, calculate and display the beginning principal balance, the interest earned, and the final principal balance for the quarter.For example: The user entered 1000.00 for the beginning principal balance, 5.25 for the interest rate, and 8 for the number of quarters. The output from this part of the program should be similar to the following:
Q| Beginning Principle| Interest Earned| End Principle
1| $1,000.00 | $13.13 | $1,013.13
2| $1,013.13 | $13.30 | $1,026.42
3| $1,026.42 | $13.47 | $1,039.89
etc
Here is the code I have so far, and I just am not quite sure where to go next.
Code:
{
cout << "Quarters" << " " << "Beginning Principles" << " " <<"Interest Earned" << " " <<"End Principal" << endl;
endprin = balance + (quarter * interest);
interest = quarter * interest;
cout << endprin << endl;
}
View 2 Replies
View Related
Jun 1, 2014
This is a test program that takes a number of arguments from the command prompt and concatenates them into a string object. I was looking into the possibility of using the range-based for loop for this purpose. Can it be done with pointer based arrays? I am mainly doing this because I want to have a firm understanding of range-based for, but also would like to do this with least amount of code possible.
This is my working program:
#include <string>
#include <iostream>
int main(int argc, char *argv[]) {
if (argc > 1) {
std::string concatenatedArgs;
[Code] ....
Can I somehow replace my while-loop with a range-based for? I tried the following but the compiler points out that begin and end were not declared in the scope of the range-based for loop.
#include <string>
#include <iostream>
int main(int argc, char *argv[]) {
if (argc > 1) {
std::string concatenatedArgs;
[Code] ....
View 3 Replies
View Related
Jan 16, 2013
it seems that I cannot define a method of an inner nested class if it is a private class. for example:
class outter {
class nested {
void foo ( void ) {} // okay - but is this inline?
} void inner::foo( void ) {} // not okay - cannot define inside another class
} void outter::inner::foo( void ) {} // not okay - 'nested' class is private!
what I want to know is, is there another way to define an inner class's method? and if not, is it eternally doomed to be inline because it has to be declared inside it's own class declaration?
View 5 Replies
View Related
Jun 25, 2014
I am having problems invoking methods in a nested class using reflection. I have the following:
A parent class, Group, that holds instances of a simple class called Signal. I want to modify the number of instances inside the group class often. So, all my code has to be dynamic and use reflection to know how many instances of signal there are inside the Group class.
class Group{
public static Signal name1 { get; set; }
public static Signal name2 { get; set; }
public static Signal name3 { get; set; }
[Code]....
I had no luck invoking the method of the instances of signal class that are inside the Group class. I tried getting the methods name using getMethods() but could not navigate through the syntax.
How could I invoke and pass parameters to the method of the instances of signal using reflection? Is there a better way of accessing the properties and methods of nested classes?
View 4 Replies
View Related
Feb 13, 2013
I am having problems understanding where this code is wrong as I believe that I am not receiving the correct outputs. I have truncated the code to the most pertinent.
The algorithm asks that every value in array1 be divided by array2
and if that value is less than the ratio * array3 (percentage filter) then output.
I have no problem with the code with the exception of abs(ratio*array3[m]
Assuming that the ratio = 1 then all values get outputted, I want the array1 (first value) to have the first value in array3 assigned to it, the second value in array1 to have the second value in array3 to be assigned.
So:
array1 (1.0) with array3 (0.05)
array1 (2.0) with array3 (0.06)
etc.
or
array1 (1.0) with array3 (0.05)
array1 (3.0) with array3 (0.07)
Code:
array1[5] = {1.0, 2.0, 3.0, 4.0, 5.0};
array2[5] = {1.0, 2.0, 3.0, 4.0, 5.0};
array3[5] = {0.05, 0.06, 0.07, 0.08, 0.09};
ratio = abc/def;
for(int m=0; m<5; m++) {
[code]...
View 2 Replies
View Related
Nov 25, 2013
Is there a way to tell the program to continue reading the next line of code within a nested IF statement?
The reason I want to do this is because the two "else" statements in the following sample (the main and the nested else) will contain the same exact code and I don't want to repeat it twice. I know I can do this by creating a function and calling it from each else statement but I was just wondering if what I'm asking is even possible without using a function.
if(1 < 2) {
// yes 1 is less than 2
if(5 > 10) {
// do something
} else {
// no, 5 in not greater than 10
// here is where I want to tell the program to continue reading the next else statement
[Code] ....
View 8 Replies
View Related
Jul 27, 2013
I have a structure for 4 divisions, and can't figure out how to loop through it.
So I ended up writing everything for times.
If I had 500 divisions I wouldn't be able to get away with it!
How can I have the structure in array form so I can fill it via looping?
/*Headers*/
#include <iostream>//needed 4 input/output
#include <iomanip>//needed to round
#include <string>//needed 4 strings
using namespace std;//global namespace to avoid name clash
struct division {
[code].....
View 7 Replies
View Related
Apr 24, 2015
For a Texas holdem' project I need to be able to draw out rectangles using ASCII codes and for loops. I started the code
#include <iostream>
#include <string>
#include <Windows.h>
[Code].....
View 6 Replies
View Related
Jul 21, 2014
I have a very simple program the time complexity of the function that I used in this program is O(mn)because it has a nested loop, I really need to reduce the time complexity to O(n)
[code=c++]
#include <iostream.h>
#include<stdlib.h>
int *char_count( const char* DNA, const int *starts, const int *ends, char letter);
int main()
[Code].....
View 5 Replies
View Related
Sep 30, 2013
I am using
#include <vector>
#include <map>
In my c++ project, and these 2 header files are included in almost 200 .cpp files and when i build a static library for the whole project in linux its size is around 10MB and when i did the same thing in windows its size is almost over 80MB, and one thing i noticed was both vector and map combinely takes 500KB in each .cpp file in windows. I am building the static library in windows using release mode.
I am using VS 2007. Any way to reduce the library size.
View 2 Replies
View Related
Jun 19, 2014
I am looking at reducing the size of a compiled executable for easier distribution.
What factors affect the size of an output executable?
Would literally having defined and implemented less functions, would make the exec. smaller? Meaning that instead of have a DLL ( yes im on windows ), I would download the source code of a library and comment out the functions ( and code ) that I am not using -- Would this process decrease the size of my exec.?
View 2 Replies
View Related
Apr 3, 2014
I wrote this code, and everything was working well, but part of the assignment is that it must include nested loops. Once I added the nested while loop, which is basically an if statement, my life was ruined. I am trying to nest a loop in the code that will basically tell the compiler that if the value "loopVol" were to exceed the value of "final" after adding an increment, to run the program for the "final". How can I do that?
Example:
initial = 10
final = 123
increment = 10
as of now, the program would stop at 120, but I want to nest a loop that will tell the compiler to calculate at the final if this happens.
#include <iostream>
#include <iomanip>
#include <stdio.h>
[Code]......
View 3 Replies
View Related
Jan 17, 2013
I've been trying for more than one month to access a method found in a library called libcocosnas_static.a. I'm using Cocos2d-X version 2.0.4. The library has been used many times by my company to make games using cocos2d-1.0.1-x-0.12.0 without any problem.
This is what I've done:
1- I added the include paths of the library to both eclipse and Android.mk
2- Included the .h file using #include "NASPlatformUtil.h"
3- Added the libcocosnas_static.a file to the proj.androidobjlocalarmeabi folder
4- Added "LOCAL_WHOLE_STATIC_LIBRARIES += cocosnas_static" to the Android.mk file
5- Called the function using: NASPlatformUtil:: openUrl("http://xxx.xxx.com/");
I can right click on the function, click Open Declaration and get it without any problem, but the compiler keeps on giving me that dreaded error...
View 3 Replies
View Related
Feb 2, 2015
I have a question similar to the one here: [URL] .....
The main difference is I would like to pass a method of derived class as a parameter to some other method in its template base class.
template <typename BaseType>
class Base {
public:
typedef void (Base::*Callback)(int A);
[Code] .....
The above is an example which does not compile. My compiler complains that the two BaseMethod() calls in DerivedMethod() are invalid uses of non-static member function.
Is this not possible to do, or is my syntax simply wrong? All I want is to be able to pass as an an argument to a method in the base class from the derived class some callback as a variable for the base class to invoke later.
View 2 Replies
View Related
Apr 15, 2013
I have assignment which requires me to print out and array code and a pseudo code. I dont no what a pseudo code is,.
View 2 Replies
View Related
May 21, 2013
here is a nested stl map
typedef struct struct_RISCP {
float nSCPTotal;
int nSCPCount;
CString strIMEI;
std::map<UINT8, int> mapSection;
}RISCPSt;
map<stRncCellIdntyDmnType, RISCPSt>m_mapRISCP;
it occupied too much memory,i wanted to clear them(both outer map and inner map) ,some one told me you just need to call m_mapRISCP.clear(),then the mapSection (inner map) will be cleared automaticly,in other words, m_mapRISCP.clear() will clear both outer map and inner map.
View 5 Replies
View Related