C++ :: Decipher Program - Nested Loop
Feb 17, 2013
I'm writing a series of basic decipher programs and I have run into an issue where I get the correct answer when I start the loops at the iteration that contains the correct answer.
Code:
// generate key "words" with length of 3
for (int x = 0; x < 26; x++){
for (int y = 0; y < 26; y++){
for (int z = 0; z < 26; z++){
[Code] ....
This is the essence of the loop, I've attached the program in its entirety if necessary. Basically what happens is if I start the loops at x = 17, y = 7, z = 12, then I get the correct decipher shifts but if I start at 0,0,0 whenever it gets to that iteration (12,000 ish) the shifts are off by 2 or 3. "koq" should translate to "the" but im getting "dcz". Is this a simple bug in the or is something moving to fast for something else to keep up?
l3_ws.txt
main.cpp
View 1 Replies
ADVERTISEMENT
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
Mar 25, 2013
This code is used in scientific calculation for optimization problem.
Basically a particle is moving in a three dimensional space, its position is (x,y,z).
At each position, there is a fitness value associated with that position.
The fitness value is given by fitness(x,y,z) (code line 12~19).
We need to find out, when the particle moves around randomly, what is the highest possible fitness value.
To solve this, below code is used, and it produces correct result.
#define DIMENSION 3
#define MAXX 4
#define MINX 0
#define MESHsize 1
#include <iostream>
using namespace std;
float maxValue = 0.0;
[Code] ....
The output of the code:
[ fitness(0,0,0) = 0] [ fitness(0,0,1) = 1] [ fitness(0,0,2) = 4] [ fitness(0,0,3) = 9]
[ fitness(0,1,0) = 1] [ fitness(0,1,1) = 2] [ fitness(0,1,2) = 5] [ fitness(0,1,3) = 10]
[ fitness(0,2,0) = 4] [ fitness(0,2,1) = 5] [ fitness(0,2,2) = 8] [ fitness(0,2,3) = 13]
[ fitness(0,3,0) = 9] [ fitness(0,3,1) = 10] [ fitness(0,3,2) = 13] [ fitness(0,3,3) = 18]
..... so on
Answer: highest fitness = 27
Note: In this case, the values of x,y and z is integers from 0 to 3 inclusive.
For 3-dimensional space above, actually the code had run through 3 nested "for" loops.
Question: Above code works for 3-dimensional space. How to generalize the code, so that it works also for N-dimensional space, where N is an arbitrary integer?
( Note: possibly N = 30 )
View 19 Replies
View Related
Nov 3, 2014
I'm trying to output a pattern using loops. The pattern is a plus symbol made of 3 rows of 5 x's followed by 3 rows of 15 x's and finally 3 rows of 5 x's.
I can get the program to output all 9 rows with 5 x's but I don't know how to do the 3 rows of 15 in the middle. I have tried a while loop and also an if statement but nothing seems to work.
#include "stdafx.h"
#include <iostream>
#include <iomanip>
[Code]....
View 8 Replies
View Related
Mar 14, 2014
Code:
while(x==1){
for (i=0;i<j;i++)
{if (word1[i] == word2[i])
{prefix[i]= word2[i];
counter++;}
else
x=2;}
Basically after the 3rd run of the for loop, it encounters a contradiction. I want it to exit right there and then. Instead it continues to run the for loop. What can I do?
View 4 Replies
View Related
Feb 9, 2014
I was wondering how I could use a continue statement that continues in a nested loop. For example if I have
for (int i=0;i<amount;i++) {
for (int j=0;j<I[i];j++) {
for (int k=j+1;k<I[i];k++) {
if (P[i][j]+P[i][k]==C[i]) {
//What should be here?
}
}
}
}
If the condition is met then the most outer loop (in i) should continue to the next iteration.
If i simply fill in continue; in before the comment then it only continues the loop in k so that is not what I want.
View 5 Replies
View Related
Mar 13, 2013
How to make triangles using c++, in nested while loops.
The triangles were:
*
**
***
****
*****
******
*******
********
*********
**********
I have made a code for this:
int counter=1;
while (counter<=10){
int counter2=1;
while (counter2<counter){
[Code] .....
I was not quite sure about this one.
View 5 Replies
View Related
Jan 6, 2014
how to draw a flow chart for following nested loop?
for(int r=1;r<=5;r++){
for(int c=1;c<=r;++c){
cout<<c;
}
cout<<endl;
View 1 Replies
View Related
Mar 17, 2013
I am attempting to read a file with 2 numbers in it. The first indicates the number of rows the second, the number of columns. So for a file (Data.txt) that contains the numbers 5 7, I want to display
0 0 0 0 0 0 0
1 1 1 1 1 1 1
0 0 0 0 0 0 0
1 1 1 1 1 1 1
0 0 0 0 0 0 0
and write that output to a file.I can display the correct number of rows and columns but I can't figure out how to display alternating rows of 0's and 1's.
#include <iostream>
#include <fstream>
using namespace std;
int main() {
ifstream inFile;//declare file
[code]....
View 5 Replies
View Related
Feb 28, 2013
What does the following nested loop output ?
count = 1;
while (count <= 11) {
innerCount = 1
while innerCount <= (12 - count) / 2) {
cout << " ";
innerCount++;
} innerCount = 1;
while (innerCount <= count) {
cout << "@";
innerCount++;
} cout << endl;
count++;
}
View 9 Replies
View Related
Jun 3, 2014
I would like to do something like this:
for (int i=0; i<5; i ++)
{
for (int j=0; j<5; j++)
{
//* CREATE A NEW FILE FOR WRITING * //
}
}
I don't know how to create a new file that doesn't get overwritten each time the loop runs.
View 13 Replies
View Related
Nov 15, 2012
My program involves trajectory planning using cubic spline method for a robotic arm. In the process, I had to calculate joint angles for each point in the path. In the last few lines of the code I need to write the values for counter and theta1 into a text file which I called "Test.txt". I am doing this using a nested for loop(the counter runs until it reaches 19 and hence need 19 theta1 values corresponding to it). However, I can't get all the theta1 values transferred to the text file.The statement within my inner loop is wrong and don't know how to fix it.
for(i=0;i<num_via;i++){
current_time = GetTickCount();
//joint[0] = mult_joint[i][0];
//joint[1] = mult_joint[i][1];
//joint[2] = mult_joint[i][2];
//joint[3] = mult_joint[i][3];
[code]....
View 3 Replies
View Related
Oct 29, 2014
We were discussing how to find average time complexity of different algorithms. Suppose I've a the following nested loop
for(int i=0;i<n;i++)
{
min = i;
[Code].....
Now the outer loop will iterate N times. the inner loop will always iterate 3 times no matter what the value of N is. so the worst case time complexity should be O(n^2) but what about the average case and the best case? I mean in terms of searching we can figure out and distinguish between the worst,best and average as it depends upon the number of comparisons. But here how can the average and best case be different then the worst case.
View 13 Replies
View Related
Jan 20, 2014
So I have this problem with not being able to iterate over a vector in a nested for-loop. Here's the nested for-loop:
bool innerHit = false;
for (std::vector<Sprite*>::iterator outerIter = sprites.begin(); outerIter != sprites.end() && (!sprites.empty()); outerIter++) {
Sprite* spriteOne = *outerIter;
for (std::vector<Sprite*>::reverse_iterator innerIter = sprites.rbegin(); innerIter != sprites.rend() && (!sprites.empty()); innerIter++) {
Sprite* spriteTwo = *innerIter;
[Code] .....
What happens is, after having called the collisionDestroy-function and the program tries to execute the nest loop in the outer for-loop, it all crashes with the text "Expression: vector iterator not decrementable", which I understand is because the iterator will have already become useless. The question is: know this, how do I fix it? I can't seem to get a hang of it.
Here's the collisionDestroy-function (the collisionReaction does nothing but sets a few local variables):
void Enemy::collisionDestroy(std::vector<Sprite*>& sprites) {
for (std::vector<Sprite*>::iterator iter = sprites.begin(); iter != sprites.end(); iter++) {
Enemy* tmp = dynamic_cast<Enemy*>(*iter);
if (this == tmp && collisionType == 3 || collisionType == 1) {
sprites.erase(iter);
break;
}
}
}
View 14 Replies
View Related
May 4, 2014
Suppose I'm given the following code:
int mystery(int n){
int something;
if(n > 0)
View 1 Replies
View Related
Nov 17, 2013
I need making a function that is deciphering multiple strings from a .txt file. The function is supposed to remove all instances of the first three characters from the string.
An example would be from l.pjkjsdfl.p[)sdfslkl.p ------> jkjsdf[)sdfslk
Basically it removes "l.p" from wherever it is in the string.
I am new to C++ and I don't know where to start from.
View 1 Replies
View Related
Nov 5, 2013
You are to create a program using nested looping. Your program is to have a menu asking the user to select which pattern to create and how many rows to use (it should accept 1 to 10 rows and keep the aspect ratio of the pattern). The patterns are 1 - Box, 2 - V, 3 - Inverted V. Note, if I select pattern 1 with 10 rows, the box will have 10 asterisks in the first row.
Okay so i came up with the box but i have been getting stuck at getting the V or inverted V.
View 1 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
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
Oct 13, 2014
We are making a program--but every time we input a value for scanf, the following for loop does not work and the program quits without displaying the for loop's function. We are not getting any errors.
View 11 Replies
View Related
May 11, 2013
I am having problems at the end of the program with the do-while loop to restart the program and the validation.
#include <iostream>
#include <string>
#include <iomanip>
#include <cstdlib>
#include <time.h>
using namespace std;
[code]....
View 3 Replies
View Related
Apr 28, 2014
I need starting a do-while loop in my program that runs up to 10 time that asks the user to enter up to 10 scores, which include the slope and rating for the course, and calculates the handicap for each score.
Here's what I have so far:
Code:
{
//This program calculates a golfers handicap.
#include <iostream>
#include <string>
using namespace std;
[Code] ....
View 1 Replies
View Related
Feb 21, 2015
I've just started to learn C programming. In following program I need to use a loop, so at the end, it asks the user that if wants to try other numbers or not. If yes program restarts from beginning.
Code:
/*
A program who checks three positive integers to be 3 sides of a triangle and calculates the area and shows the triangle type.
*/
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
int main(void)
[Code] .....
View 11 Replies
View Related
Feb 27, 2014
3. Write a program that reads a sequence of positive integers and prints out their sum, except that if the same number occurs several times consecutively, ignore all but the first. Assume an input of 0 marks the end of the input. For example, if the input is 3 8 5 5 4 9 1 1 1 1 8 0 then you should print 38 (i.e., ignore one of the 5's and three of the 1's).
View 5 Replies
View Related
Nov 20, 2014
I'm trying to write a program for a lab, I know how easy it is but cant see to crack it.
I need to make a program using while or for loop.
I must have the user type a number, for an input value an output value but then do it again several times but without it going on forever asking again and again.
View 4 Replies
View Related
Aug 29, 2014
How to make a loop so program never end, it end only by typing "EXIT"
View 1 Replies
View Related