C/C++ :: Array With Two Variables And Loop

Feb 18, 2014

I have two variables t and x in an array that alternate. I want to add two t's and find the average and then do the same for the x's. After that, I want to find the slope of x -> t. My problem is that I specify the n for t, but since it's local I can;t use it for the slope. Here's my code so far:

#include <iostream>
using namespace std;
int main() {
double PA [8] = {0.0001234, 1.0005434, 0.0005678, 1.0023423, 0.00063452, 1.0001546, 0.00074321, 1.00017654};

[Code] ....

And, can I actually use the n2 as an index, or will the processor not understand that?

View 7 Replies


ADVERTISEMENT

C :: Cannot Get Out Of Loop - Stuck With Updating Variables

Jul 18, 2013

Code:
#include<stdlib.h>#include<stdio.h>
#include<unistd.h>
#include<math.h>
int main(void)
{
double N, NG, LG, epsilon, root; // setting all variables to type double

[Code] .....

The goal is to create a program to calculate the square root of a number provided by the user to an error tolerance 0.005

Looking around i fond the Code: fabs(NG - LG) < epsilon); section that was very similar to what i was using, but if this is better im down for that.

My issue, from what i can see, is the updating of the values of LG. if the test for error tolerance fails then LG needs to take on the value of the results of NG.

Now I am not 100% that is the point of failure due to the fact the script does calculate the root properly, but it never exits the program once it reaches the tolerance level.

Code: imac:ENG-3211 user$ ./hw_4_1

Please enter the number you wish to find the square root: 4

4.00 2.500
4.00 2.050
4.00 2.001
4.00 2.000
4.00 2.000
4.00 2.000
4.00 2.000
4.00 2.000
4.00 2.000

And it continues forever until i manually break the program even though the answer was found, in this example, on the 4th loop.

View 8 Replies View Related

C++ :: Variables Declared - How Many Times Loop Executes

Jan 27, 2015

Assume all variable are declared correctly, the following for loop executes how many times?

for (i = 0; i <= 20; i++)
cout << I;

I think 20 but am not sure.

View 7 Replies View Related

Visual C++ :: Re-input Variables After Loop Ended

Oct 18, 2014

I just started programming and want to make the game cows and bulls. The problem is that after the loop has ended and both vectors are not the same I want the user to 're-input' the starting variables a,b,c&d but I don't know how to.

Code:
#include "std_lib_facilities.h"
int comparison(int a, int b, int c, int d)
{
vector<int>bc={ 1, 2, 4, 9 };

[Code].....

View 2 Replies View Related

C++ ::  Vertical Histogram - Variables In For Loop To Print Stars

Dec 6, 2013

I want to create a vertical histogram in my code. I already made it go vertical but not the way I want it.

Example:
I want it like this:

Range1 Range2 Range3 Range4

And asterisks under each one, depending on the user input. (My code is below and doing it on here doesn't make it come out correctly)

But what I've managed to do is this:

Range1
*
*
*

Range2
*
*

Range3
*
*
*
*

Range4
*
*

Which I don't want. I want everything else to stay pretty much the same since I can only use some features such as Arrays and really basic functions.

Here is my code: (Worked fine last time I used it and I am doing it on Visual Studio 2010 (at uni) and 2013 (on my laptop)).

#include <iostream> //Start of code
using namespace std;
int MarkValueInput; //Mark entered by user
//Counter variables for ranges in While Loop
int counterlow; //Counter for low range
int countermidlow; //Counter for mid-low range
int countermidhigh; //Counter for mid-high range
int counterhigh; //Counter for high range

[Code] .....

View 4 Replies View Related

C++ :: Loop Program To Go Back Without Losing Values Of Variables?

Mar 30, 2014

I know that returning to main() is not a good idea but how will I loop the program to go back to the position selection when the voter is done voting for the last position.

when I run the program it seems fine. getting the votes from President to PRO Position is fine. The problem is how will the next voter vote without losing the vote (tally) of the previous voter?

FLOW:
SELECT POSITION (a-e) ---> SELECT A CANDIDATE (a-c) ---> (this goes on until the position of PRO) ---> ( then go back to the POSITION SELECTION)

After every vote there is a case statement for which I can choose to vote for the next position, quit, or show results (and after showing the results it will go to the next position to vote)....

#include<iostream>
#include<string>
#include <conio.h>
using namespace std;
int pca=0,pcb=0,pcc=0,ptv=0;

[Code] .....

View 1 Replies View Related

C :: Automatic Declaration Of Array Variables

Jul 14, 2014

I have some files:

file1: 1000x500 array of numbers
file2: 2000x600
file3: 300x5000
...

I would like to automatically declare array variables of myarray1, myarray2, myarray3... that reads the numbers from files:

Code:
for (i1=0; i1<nrows; i1++)
{
for (i2=0; i2<ncolumns; i2++)
{
fscanf(filename, "%lf", &y);
myarrayX[i1][i2]=y;
}
}

nrows and ncolumns can be passed from a config file. Also I can cat all files into a single filename. However, the difficult part is:

How to declare different myarrayX automatically ? (A workaround is to declare a huge 3d array but I do not want to do this).

View 4 Replies View Related

C :: Printing Sum Of Variables From Array Of Structure?

Oct 10, 2014

I have an array of structure that takes family information (name, age, and state) after it takes the users input it prints back all of the information that was input and then prints the family members that just live in Texas. After that I am trying to implement a function to average out the ages of all family members but I cant seem to get it right, whenever it gets to that part of the program it just outputs 0. I also tried adding an & sign prior to FAMILY[i].age and got an error of int from ptr with no cast.

Also I realize that the code only shows adding of the family members ages which is fine for now as I am concentrated on getting that to work in the first place.

Code:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//Global Variables
int i=0, a=0, sum=0;

[Code].....

View 8 Replies View Related

C :: Rotating Array And Assigning It To Different Variables

Feb 3, 2013

I have a problem with rotating an array and assigning it to different variables. This is wat i have done.

Code:

#include<stdio.h>#include<stdlib.h>
#include<conio.h>
#include<math.h>
int n[16],a[16],n1[16],n2[16],n3[16],n4[16],n5[16],n6[16],n7[16],n8[16],n9[16],n10[16],n11[16],n12[16],n13[16],n14[16],n15[16],n16[16], c=0;
int x[16],i,j,k;
int rotate();

[code]....

View 4 Replies View Related

C++ :: How To Save Two Digit Variables In Array

Feb 11, 2014

I am trying to redefine an array, but the result can only display one digit variable. I want the program to display 97 98 99 *+, but the program is only displaying 789*+.

int main() {
string test="abc*+";
for(int i=0; i<test.size;i++)

[Code].....

View 1 Replies View Related

C++ :: Passing Array Of Variables By Reference?

Sep 10, 2014

I define an integer variable and pass it by reference to a function.

int func(int &x)
{
// do something
}
int x;
func(x);

However, I have not only one variable but an array of variables with its predefined name. How do I pass to the function by using loop? Example:

int x, y, z;
func(x);
func(y);
func(z);

How can I do this by using loop?

View 7 Replies View Related

C++ :: Char Array Variables In If Statements?

Mar 25, 2013

I'm currently doing the exercises for the fifth chapter (Loops) and I've done all of them, but I wanted to go the extra mile on the last program I'm supposed to design. The program is a poll and all the input from the user will be with numbers. However when a letter is pressed then of course you get wrong behaviour from the program, it keeps looping endlessly.

Here is a fragment of what I think is the way of doing it - but of course it's not working

Code:
int p = 0
char anyLetter[]={"abcde"};//Initializing char variable
char a = anyLetter[p];
else if (userAnswer[n] == a)//if statement where char needs to be used.
{
cout << "Pressing a letter maybe? It's only with numbers. Try again." << endl;
continue;
}

View 5 Replies View Related

C++ :: Force Variables To Be Stored In Char Array

Jan 7, 2015

I want to declare a char* array, and then make any future variables declared to be stored in a specific location within the char* array. Is this even possible, and if it is how would I go about doing it. (I plan on storing any primitive data type in it (not classes or structs), and they may signed or unsigned).....

View 12 Replies View Related

C/C++ :: Force Variables To Be Stored In Char Array

Jan 7, 2015

I want to declare a char* array, and then make any future variables declared to be stored in a specific location within the char* array. Is this even possible, and if it is how would I go about doing it (I plan on storing any primitive data type in it (no classes or structs), and they may signed or unsigned). I want to be able to use the variables like any other variable, I just want the variable's address to be within the char* array.

I am trying to make a program that is similar to a virtual machine and an emulator put together, and it can only run one os (which will be hard-coded into to the program). The reason I wanted to do this is because it would be the easiest way to make sure that all variables in memory are in one contiguous block, that way the part that manages memory wouldn't have to store the locations of each variable (which would have been necessary for the virtual memory manager).

An example of what I am wanting to do is

char* ram [256]; // Address 0x00 to 0xff

// Code to make sure that new variables' addresses are in ram[] if necessary

unsigned short a = 5; // Gets stored at address 0x00

unsigned int b = a; // Gets stored at address 0x00+sizeof(a)

View 1 Replies View Related

C++ :: Using Loop To Process Array Of Characters Starting From Beginning Of Array?

May 3, 2014

Assume you want to use a loop to process an array of characters starting from the beginning of the array. You want the loop to stop when you read the null terminator character from the array. Fill in the loop test condition that will make this work correctly.

index = 0;
ch = array[index];
while ( _____________________________)
{
// process the character
index++;
ch = array[index];
}

View 1 Replies View Related

C++ :: Array Container For Loop?

May 13, 2014

I want to find all instances of a substring mysub in array container myarr and replace each occurrence of mysub with empty string " ". To do that, I'd like to use for loop with search algorithm.

Code below:

p=array iterator
for ( p=search(myarr.cbegin(),myarr.end(), mysub.begin(),mysub.end();
p!=myarr.end();
p=search(p,myarr.end(),mysub.begin(),mysub.end() ); {

[Code] ......

View 2 Replies View Related

C :: How To Calculate Sum Of Two Lines In Array (for Loop)

Mar 19, 2013

I'm having trouble figuring out how to calculate the sum of two lines in the array(for loop)? And the next part, I can return the values to the main program however, how do I do it with c pointers?

(1) Write a C function that takes an integer array argument. The array argument has two rows and NDATA columns where NDATA is a symbolic constant. For each column in the array argument, the function calculates the sum of the values in the first and second row. The function returns to its calling program (using a second argument and call by reference as implemented with C pointers) the maximum of these sums. Additionally the function returns the column subscript where this maximum first occurs. This subscript value is returned the usual way (using a return statement). etc...

Here is the start of the array program

Code:
#include<stdio.h>
#include<stdlib.h>
#define NDATA 5 /* define number of data per row */
int row; /* count rows */
int column; /* count columns */

[Code] ....

View 2 Replies View Related

C++ :: Loop Unrolling Multidimensional Array?

Nov 16, 2013

I am trying to unroll the inner i and j loops within this multi-dimensional array which spits out a block image. Unfortunately, the image does not match the color of the original image probably because filter->get(i,j) gets altered in a way that I don't want it to.

double
applyFilter(struct Filter *filter, cs1300bmp *input, cs1300bmp *output) {
long long cycStart, cycStop;
cycStart = rdtscll();
output -> width = input -> width;
output -> height = input -> height;

[code]....

View 3 Replies View Related

C/C++ :: Using Pointers For Character Array In For Loop

Nov 3, 2014

I'm taking an intro to programming course and my assignment is to Write a very simple program that uses two for loops and ONLY pointers and pointer arithmetic (NO other variables are allowed) to display a palindrome forward then backward. Declare the palindrome as follows: char str[] = “Straw Warts”; Well my prof never did anything with char arrays and I can't quite fit make the pointers work in the loop. This is my code. It runs how it should, i just can't seem to make a working for loop. It always says that you can compare int and char

#include <iostream>
using namespace std;
int main()
{

[Code]......

View 7 Replies View Related

C :: Passing Array To Function And For Loop Crashing

Sep 23, 2014

The first problem i'm having is in the following code.

Code: #include<stdio.h>
#include<stdlib.h>
#include<time.h>
// program ask for max value and always returns random numbers < max

[Code].....

The ultimate goal of these two programs is to merge them and make one program that takes a max value from the user and fills up an array of (10^6) elements with values ranging from (0 to maxValue).

View 7 Replies View Related

C++ :: Creating Multidimensional Array In Infinite Loop?

Aug 13, 2014

From the example given below, I would like to Generate a matrix who stores a generated array for each iteration. I have an understanding of inputting single elements to a matrix but how can I input an array to a matrix. let's say I would like to input and array of 4 elements that should be stored as a single row of the generated matrix and do this for an infinite while{true} loop.

#include <iostream>
#include <unistd.h>
#include <cstdio>
#include <cmath>
#include <ctime>
#include <stdlib.h>
#include <stdio.h>
#include <vector>

void printArray(int arr[], int size) {

[Code] ....

View 7 Replies View Related

C++ :: Using For Loop To Input And Output Array Values

Jan 23, 2014

so i got this piece of code today having some slight errors with it, how its actually done as i want to know where i have gone wrong here is the code .. Using a for loop to input and output array values

*/
#include <stdio.h>
int main(void) {
/* Declare an array of integers */
int Grades[5];
int nCount;
/* Populate the array */
for(nCount = 0; nCount < 5; nCount++)

[Code]...

View 3 Replies View Related

C++ :: Reaching Data Of Different Array Using Pointer In Loop

Jul 31, 2013

I have 3 arrays named: abee1 , abee2, myarray.

I have loop that want to call abee1 , abee2 one each time and copy myarray into it . It means that I reach the name of abee1 to abee2 in the loop for doing this. I made the name of each one by using strings ( abee1 , abee2) in the loop. In the last I want to copy the myarray into the abee1/2 using pointer.

I do not know what to do after the line "string arrayname = "abee" + String;".

#include<iostream>
#include<string>
#include <sstream>
using namespace std ;
int abee1 [4] [4] ;

[Code] ...

View 1 Replies View Related

C++ :: Passing Values Into Array Using For Loop From Input File

Jul 5, 2014

I'm trying to pass these numbers into the array using the for loop: 1, 2, 3, 4, 5. I created two files in my project called "inStuff.txt" and "OUTPUTS.txt". I know I have an error at my input in the for loop, which is a bit similar to my problem or something? I'm also on linux using a crossGCC complier(not sure if that makes a difference).

#include <iostream>
#include <fstream>
using namespace std;

[Code]....

View 4 Replies View Related

C++ :: Loop Through Indexes In Guess Array And Assign M Randomly

Jan 19, 2013

Just run a loop through each of the indexes in the guessarray and assign 'M' to them randomly.

View 2 Replies View Related

C++ :: Putting Data Into Array Using For Loop From Input File?

Jul 7, 2014

I'm trying to pass several integers into the array idAr[10] from an input file using a for loop and then having them output on the console. The output should look like this:

#include <iostream>
#include <fstream>
using namespace std;

[Code].....

View 4 Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved