C :: Make A Program That Multiplies Loop Variable By 2
Nov 15, 2013
I have an exam in C programming I'm a little stuck on loops..I have to make a program that multiplies the loop variable by 2 then takes away 1... easy, that was a for loop no problem
for (x=3; x<=65; x=x*2-1)
that should print out 3 5 9 17 33 65 however, my lecturer has given the task for it to reverse, but not to display the number 17 on the way back down using an if statement...however, this time it must be a do while loop... This is what I have so far.
Code:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int x = 129;
}
[code]....
View 7 Replies
ADVERTISEMENT
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
Nov 27, 2014
Write a program to make a table for any input number and then continuesly ask to press y to print more table and if you press any key other than y then program must be terminate using while loop and do while loop. How to start or end with it.
View 2 Replies
View Related
May 1, 2013
Write a C-program that efficiently multiplies a number by a factor 2 to the power n. The number to multiply and n are variables, which get a value at the start of the program.
Clue: 1 shift to the left is the same as multiplying by 2. 2 shifts to the left are the same as multiplying by 4. 3 shifts to the left are the same as multiplying by 8
View 7 Replies
View Related
Sep 8, 2013
I have to write a loop assigning a variable x to all positions of a string variable and I'm stuck. I don't have extensive experience with arrays and I'm also a bit confused about C-String. The problem is below.
"Given the following declaration and initialization of the string variable, write a loop to assign 'X' to all positions of this string variable, keeping the length the same.
char our_string[15] = "Hi there!";
(Please note this is a 'C-string', not C++ standard string.)"
View 5 Replies
View Related
Mar 29, 2014
Suppose you a class declared with a pointer initialization variable. When writing the copy constructor, how would one make a deep copy of the pointer variable? Can it be done in the same manner as automatic variables i.e. in the initialization list or in some other manner?
I am using C++ 11.
View 14 Replies
View Related
Dec 9, 2014
I'm trying to optimize this code using openMP. The line I added made it run about twice as fast but I'm trying to figure out how to make it even faster. Could reshaping the loops potentially increase the speed?
//4 threads
int listsize=15000;
#pragma omp parallel for shared(f) private(i,j,hash)
for(i = 0; i < listsize; i++) {
printf("Thread: %d i: %zu wl_size: %zu
",omp_get_thread_num(),i,wl_size);
for (j = 0; j < num; j++) {
h = f[j] (getw(list, i));
c[h] = 1;
} }
View 2 Replies
View Related
Oct 26, 2013
I need to do make a loop inside a condition. Can it be done? I don't want to call another function to do it. Any way at all without calling separate function inside the if? I just want to do:
if (
for (int i = 0; i<=10; i++)
{
//stuff related to the for loop
} )
{
//stuff related to the initial if condition
};
View 7 Replies
View Related
Jan 29, 2015
I am wondering how I could make an array which contains arrays, but with a variable size.My first try..
Code:
int array[][] = {{1}, {2}};
But this isn't proper
View 2 Replies
View Related
Apr 4, 2013
I'm making a program for a MMO I play. It calculates the cash and resources needed to build certain 'modules'. I'm having trouble to do something very simple : make a loop until the user asks to break the loop. Here is what the interface looks like :
What module do you want to install?
(Examples: 'Avenger' or 'VLLT')
dd // I input this
DD. COST each : 60,000 cr.
How many do you want? (Modules left:15)
(NOTE : You may enter a negative number if you made a mistake)
3 // I input this
Current Total Module Price : 180000 cr
+6 droids
[Code] ....
The bigger code box below is a part of the code I made. What I deleted is uninteresting, it's just the same thing again : other resources, other 'modules'...
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
int main() {
struct resources// Defines the amount of resources and their price
[Code] ....
View 8 Replies
View Related
Dec 11, 2013
Something that does something like this
at this point run a loop for all players, in this loop use random to determine the face and depending on the face run a loop (for roll again etc) or give next user the chance.
And this loop is 2-12 people, all rolling die.
They draw 3 die out of a cup of 12.
Then they roll the 3 die, getting fish, hook, or boot on each die.
View 1 Replies
View Related
Dec 11, 2013
[URL] ....
This is my code, but how can I implement this?
View 1 Replies
View Related
Dec 31, 2013
I'm working on a code that needs to loop a switch case back to the beginning after a certain input is entered.
ex) Code:
printf("Select an option);
printf("1. Play game");
printf("2. Status");
printf("3. Exit");
scanf("%i", &userinput);
switch(userinput);
[Code]...
For my program, I want option 2 to display something, then loop back to "select and option" after the user presses enter. How would I write that? Would i use a while? do while?
View 3 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
Feb 6, 2013
I'm writing a program to sum up the even numbers in between 1 and 100, using a while loop. At the beginning of my program, I initialize a variable "sum" to 0, and a variable "temp" to 1. Afterwards I enter a loop where I determine if "temp" is even or not, and if so add it to sum. However, at the end of my program, when I print "sum", I get a result of 0. Below is my code.
Code:
#include <stdio.h>
int main(void)
{
int sum = 0, temp = 1;
}
[code]...
View 4 Replies
View Related
Jan 19, 2014
for (int raw1=0;raw1<4;raw1++){
for (int column2=0;column2<4;column2++){
unsigned char temp=0x00;
for (column1=0;column1<4;column1++){
int z1=7;
int p1[8]={00000000};
unsigned char temp2;
[Code] ....
When I print the variable output_mix_column inside the for loop as shown in the code, it prints the right output, but when I print it outside the for loop, it changes its value ....
View 1 Replies
View Related
Jul 31, 2014
My while loop is the problem. I want to use a non-recursive function to make the triangle number the sum of all whole numbers from 1 to N.
#include "stdafx.h"
#include <iostream>
using namespace std;
int triangle(int t);
[Code] ....
View 2 Replies
View Related
Feb 9, 2015
I have to make a function that i'll later be able to use for a ceasar cypher. The letters should shift a user inputted number. This is what I have so far:
char shiftChar(char c, int s) {
char ch = c;
int shift = s;
int newC;
newC = int(ch) + shift;
return newC;
}
The problem with this, is that it doesn't loop back to the start of the alphabet once i get past z.
View 2 Replies
View Related
Aug 1, 2013
Code:
#include <stdio.h>
int main(void){
int a=0;
for(;a<=10;)
int b;
return 0;
}
I have got a code like this. I don't expect to get an output but just assumed I would see the command screen until I terminated it. What I want to do is just declare a variable b in a endless loop. But what I got from the compiler is this error: error: expected expression before 'int'. I am using Code::Blocks and I think the compiler is GCC.
View 4 Replies
View Related
Apr 28, 2013
I have a problem with my code which I can't work out:
double Mi = 200*pow(10,30);
cout << "
Enter accreted mass increment in solar masses
";
cin >> dm;
cout << "
[Code] ....
Basically the loop works, but gives the wrong results.
I need, at the end of the loop, to sort of "redefine" Mi as "Mi + Macc". I then need it to repeat the loop, and at the end add another Macc so that Mi becomes "Mi + Macc + Macc", etc.
View 5 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
Feb 7, 2015
I want to create a loop that that generates a random number set and gives the user the option to run the loop again for a new number. I have that working but when the loop ends the variable value does not leave the loop. I tried a posttest and pretest loop.
Is it possible to declare/create/alter a variable in a loop and have that value exit the loop to use in the code later? I am still learning about global variables.
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <string>
int oneDiceSix() // Roll 1d6
{int oneDiceSix = 1+(rand()%6);
return oneDiceSix;} //end function
[code]....
View 2 Replies
View Related
Aug 25, 2014
I have this code which performs the analysis part of discrete wavelet transform. It works pretty well. However, I wish to reduce the time that it consumes even further. I did use reserve() and it worked upto few msec.
int rows = signal.size();
int cols = signal[0].size();
int cols_lp1 =(int) ceil( (double) cols / 2);
vector<vector<double> > lp_dn1(rows, vector<double>(cols_lp1));
vector<double> temp_row;
temp_row.reserve(512);
[Code] ....
View 5 Replies
View Related
Apr 4, 2013
I've been messing around with loops/functions and basic logic and come up with a small maths program. Here it is:
Code: #include <iostream>
#include <string>
float divide (float x, float y) //function to divide numbers {
return x / y;
[Code] ....
Would initializing the string 'anotherGo' to a value that makes the loop run at least once be a suitable way of doing this rather than using a do/while loop? I read that a do/while loop is a black sheep but I have come across a number of uses for it. Maybe it is just preference which one you should/could use?
View 9 Replies
View Related
Sep 4, 2013
I ran into a problem while using while loop.T he declared and initiated local int variable works well with its specified value while running through the 1st run of a while loop. It is set zero while entering the 2nd run and the following unexpectedly. However, the variable still exits. The following is the code with problem.
Code:
#include <stdio.h>
int main(void){
const int x=6;
char c='y';
[Code]....
View 10 Replies
View Related