C++ :: T Minus Loops - Variable Index Declaration
Apr 25, 2014
(while)
T minus 10 and counting
T minus 9 and counting
T minus 8 and counting
T minus 7 and counting
T minus 6 and counting
T minus 5 and counting
T minus 4 and counting
T minus 3 and counting
T minus 2 and counting
T minus 1 and counting
Declare the following index before the while loop:
int index = 10;
Correctly code a while statement below using the variable index as defined above, to produce the output shown above.
So this is what my code looks like... I also have to convert this same loop into a do while and for loop. So if I can get this one right I think the others should come relatively easy.
while (int index >= 10) {
cout << "T minus " << index;
index--;
}
View 4 Replies
ADVERTISEMENT
May 11, 2013
I read that Memory is allocated during definition of a variable and not during declaration. Declaration is something like,
Code: int x;
And definition is assigning some value to it. This is what my professor taught. My doubt is if memory is not allocated during declaration, then how the compiler successfully compiles and runs the following, which i had already tried.
Code:
#include<stdio.h>
#include<conio.h>
int main() {
int c;
int *p=&c;
printf("%x",p);
getch();
return 0;
}
The variable c is only declared. But the program outputs a memory address. Shouldn't it show an error?
View 2 Replies
View Related
Oct 4, 2014
If i declare 2 variables like this static int first, second; will both of them be declared static or will only first be declared static and second a regular variable?
View 5 Replies
View Related
Aug 2, 2014
#include<iostream>
#include<conio.h>
#include<string>
using namespace std;
class ir;
class Bank_acc {
private:
string name,type,s;
long int accno,temp,balance,in;
[Code]....
errors are:
|6|error: forward declaration of 'class ir'|
|54|error: invalid use of incomplete type 'class ir'|
|99|error: no matching function for call to 'ir::interest()'|
View 1 Replies
View Related
Feb 17, 2015
int sift(int a[], int b[], int n, int p) {
int i,k,x, tmp,index=0,count=0;
for(x=0; x<n; ++x)
b[x]=a[x];
[Code] .....
This is a function for a quicksort... Everything works; however, when I return index it returns the value for 'n'... I printed the value for index right before the function returns it and its 4, as it should be.
View 1 Replies
View Related
Jan 11, 2014
Why I take warning on this code :
Code:
#include<stdio.h>
extern int v = 0; // Declaration and definition of an external
int main(void)
[code]...
Is there any error? Why I take a warning: 'v' initialized and declared 'extern'|||=== Build finished: 0 errors, 1 warnings ===| ???
and what is the meaning of :An extern declaration that initializes a variable serves as a definition of the variable. This rule prevents multiple extern declarations from initializing a variable in different ways.
View 3 Replies
View Related
Aug 8, 2013
Code:
#include <iostream>
using namespace std;
void f() {
int x=17;
//cout<<main::y<<endl; i want to access y from main scope
}
int main() {
int y=23;
//cout<<f::x<<endl;
I want to access x from f scope is there any way for this without global declaration? specially about function scopes...
View 1 Replies
View Related
Sep 10, 2013
What is difference (memory allocation or any) between declaring a variable inside or outside the variable
program1:
#include<stdio.h>
int main() {
int i;
for (i=0;i=<100;i++) {
[Code] .....
Difference b/w program1 and program2. Its look both are same. but my compiler shows something difference.
View 1 Replies
View Related
Aug 27, 2014
im doing a program to store name, age, time and fitness. and i need to hold a table of 5 such records.can i do this?
#include <iostream>
using namespace std;
int name1, age1, time1, fitness1;
int name2, age2, time2, fitness2;
int name3, age3, time3, fitness3;
int name4, age4, time4, fitness4;
int name5, age5, time5, fitness5;
[code].....
View 3 Replies
View Related
Feb 3, 2013
I got the following lines of code from a book. The variable char c is being used here to demonstration local variable declaration.
while(char c = cin.get() != 'q') {
cout << c << " wasn't it" << endl;
if(char x = c == 'a' || c == 'b')
cout << "You typed a or b" << endl;
else
cout << "You typed " << x << endl;
}
It compiles and runs. There are two issues when I try to run it.
1) It seems to loop through two times for every entry. If I insert cin.ignore() just before the closing bracket, it seems to work better.
2) the variable c does not seem to have the character I entered when examined in the if statement.
What is happening with the variable c inside the while loop scope?
Does c actually get initialized?
View 2 Replies
View Related
Nov 14, 2013
When a declare a string e.g.
Code:
char str[30]; as a global variable, the srting is initialized to NULL.
But if I declare char str1[30] INSIDE main(), then the string has garbage inside.... Why this happens??
E.g. the following code snippet...
Code:
#include <stdio.h>
char str[50];
int main(){
char str1[50];
[Code] ....
View 4 Replies
View Related
Aug 7, 2013
I wrote the following program to initialize a string after the variables is declared, but it isn't working. A warning is given by the compiler, and the execution of the program shows a strange string. How do I initialize variable word in a separate statement from its declaration?
Code:
#include <stdio.h>
int main( void )
{
char word[20];
*word = "Hello";
printf( "The string is %s.
", word );
return 0;
}
View 3 Replies
View Related
Feb 19, 2015
I'm having to do a little c++ (coming from java) and don't understand the syntax of the following declaration
Code:
SensorBase* const sensor(mSensors[i]);
It looks like it's declaring a const pointer to a SensorBase object but I don't understand how that applies to sensor(mSensors[i]) which looks like a function??
View 6 Replies
View Related
Mar 17, 2013
I am getting a compilation error from the code below. It is when i am naming a variable with my user defined type.
#include<iostream>
#include<cstring>
#include<cstdlib>
using namespace std;
class person {
[Code] .....
C:Dev-CppTRIAL.PASS.!!!.cpp In function `int main()':
66 C:Dev-CppTRIAL.PASS.!!!.cpp expected primary-expression before "p"
66 C:Dev-CppTRIAL.PASS.!!!.cpp expected `;' before "p"
74 C:Dev-CppTRIAL.PASS.!!!.cpp `p' undeclared (first use this function)
(Each undeclared identifier is reported only once for each function it appears in.)
83 C:Dev-CppTRIAL.PASS.!!!.cpp `X' undeclared (first use this function)
View 4 Replies
View Related
Feb 20, 2015
I'm using an animation program. In this program I've simulated a particle system. The particles are flying around at different and varying speeds. I've attached birds to the particles and I want to be able to control each bird's flapping animation based on its velocity; so birds moving faster will be flapping faster.
Initially, the bird's flapping animation is controlled by a parameter that goes from 0 to 100%. So not only do I need to drive the speed at which the animation goes from 0 to 100%, I need to set it on a loop so once it reaches 100%, it loops back to 0%. I'm extremely new to code so I don't think it would be wise for me to even provide a jumping off point, not that I could.
View 8 Replies
View Related
Aug 4, 2013
Is there a simple notation to check if a value is within a plus or minus range?
E.g.
//I read a value A. delay(50); //Read value again -calling this value B delay(50);
//Read value again -calling this value C delay(50); //Read value again -calling this value D delay(50);
//Read value again -calling this value E
Check IF first value A is within 5 of the value B and within 5 of value C, etc.
I can think of a few round about ways of doing this but is there any simple "equals to plus or minus" notation? (what I actually want to do is to check a lot more values than this and it will get very complicated with any of my solutions)....
View 4 Replies
View Related
Feb 5, 2014
I'm creating a bank system. So I know to make a deposit to the balance, which add ups what is the balance to i have add.
When i run the deposit function, its work well in some ways. If balance(text file) has the value 10, add i addSum 20, the balance will become 30, same as the text file will become 30. so its work well to add positive number.
double deposit(double balance){
double addSum = 0;
system("CLS");
cout<< "Welcome to deposit."<<endl;
cout<<"Enter a sum you wish to add to your account:";
[Code] .......
When I withdraw from 30 which is the balance, then i takeSum, for example i take away 30. The balance will become 30 - 30 = 0
When i make another withdraw from example -150, it will be -150.
Which shows correct.
But when i make a deposit from -150 and i addSum 130, the balance shows -500, and it should had been -20.
double withdraw(double balance) {
double takeSum = 0;
system("CLS");
cout<< "Welcome to withdraw."<<endl;
cout<<"Enter a sum you wish to take away from your account:";
cout << balance << '
[Code] .....
What is causing this problem, also when function deposit and withdraw close, it goes to readBalance function, should go to menu.
double readBalance(double balance) {
int option;
system("CLS");
cout<<"Welcome to balance."<<endl;
cout<<"Your balance is:"<<endl;
[Code] .....
View 1 Replies
View Related
Feb 5, 2014
I'm creating a bank system.
So I know to make a deposit to the balance, which add ups what is the balance to i have add.
When i run the deposit function, its work well in some ways. If balance(text file) has the value 10, add i addSum 20, the balance will become 30, same as the text file will become 30. so its work well to add positive number.
Code:
double deposit(double balance) {
double addSum = 0;
system("CLS");
cout<< "Welcome to deposit."<<endl;
cout<<"Enter a sum you wish to add to your account:";
[code]...
When I withdraw from 30 which is the balance, then i takeSum, for example i take away 30. The balance will become 30 - 30 = 0 When i make another withdraw from example -150, it will be -150.Which shows correct.
But when i make a deposit from -150 and i addSum 130, the balance shows -500, and it should had been -20.
Code:
double withdraw(double balance) {
double takeSum = 0;
system("CLS");
cout<< "Welcome to withdraw."<<endl;
cout<<"Enter a sum you wish to take away from your account:";
[code]...
View 2 Replies
View Related
Mar 9, 2013
I have same question as posted by holla and Iam not sure about merging the contents of 2 sorted arrays into another array without duplication of values.
View 9 Replies
View Related
Apr 7, 2013
I'm writing a program to calculate a final grade by adding 4 numbers minus the lowest grade and dividing by 3. My knowledge in c is not extensive I thought that a simple assigment operator would do the job but I'm getting a strange large numbers in the execution.
Code:
#include <stdio.h>
#include <stdlib.h>
main(){
int eg, g1, g2, g3, g4, fg, s1, s2, sg;
[Code] ....
View 4 Replies
View Related
Aug 23, 2012
I have an std list of type double.. and the list is always guaranteed to have just 2 elements. I need to get the value of element 2 minus element 1. What is the least amount of code to accomplish that?
I tried this:
Code:
list<double> dList;
dList.push_back(1.0);
dList.push_back(2.0);
list<double>::iterator iter = dList.begin();
list<double>::iterator iter2 = dList.end();
double result = *iter2 - *iter;
But this code does not work. Why not?
View 6 Replies
View Related
Apr 17, 2014
I was trying to debug a code which was behaving in an abnormal way
Code:
#define NOOFELEMENTS 32
unsigned char array[NOOFELEMENTS];
unsigned char array1[23];
init() {
for(i=0;i<=32;i++)
{
array[NOOFELEMENTS] = 0
}
}
I could trace the above one of the mistakes where the array initialization is crossing the array limits and writing into array[32] which is not available. My question does it overwrite into array1 as it is declared below array or it can write into any other location.
View 4 Replies
View Related
Sep 9, 2014
I am trying to get the index of a value by inputting the value:
For example if i have
int arr[5]={22, 85, 58, 78, 69};
indexOfItem(arr, 78);
and have the output be:
the index of the item is: 3
View 3 Replies
View Related
Dec 16, 2013
If i have current index and row/columns count how do i get on which row and on which column index is individually?
This is what i have tried, but column index is incorrect:
#include <iostream>
int main()
{
int rows = 5;
int colm = 6;
int inx = 12;
int rowInx = inx % rows;
int colInx = inx % colm;
std::cout << "row: " << rowInx << std::endl;
std::cout << "col: " << colInx << std::endl;
return 0;
}
View 4 Replies
View Related
Oct 11, 2014
I am trying to make a menu program and was wondering if there is a way to declare something more than once without using a different word.
ex.
#include <iostream>
using namespace std;
double grams, ounces, inches, feet, meters; //units to convert
int choice; //menu choice
int main() {
cout << "Welcome to Measurement Converter" << endl;
[Code] .....
I dont really know how to explain it but im trying to use int choice to make choose a program from a simple list.
View 4 Replies
View Related
Jun 27, 2014
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Chapter7Problem13
[Code] .....
I keep getting an out of bounds error in my code
I'm suppose to enter number of orders then enter those orders then calculate a discount and net price display the orders, discount, net price then the totals at the bottom ....
View 5 Replies
View Related