C :: How To Traverse Array Which Holds 3000 Numbers
Oct 23, 2013
I have this array which holds 3000 numbers and is initialized by assigning a random number to each element.
Code:
#include <stdio.h>
#include <stdlib.h>
int main (void)
{
int x[3000] = {rand()};
return 0;
}
I have to traverse this array so that each random value lies between -3000 and +3000 inclusive.I'm not sure how to traverse this, there is nothing in my notes discussing traversing.
View 4 Replies
ADVERTISEMENT
Oct 23, 2013
Here are the steps to this program:
1. Declare an array that will hold 3000 numbers
2. Initialize this array by assigning a random number to each element in the array
3. Traverse the array, modifying the current contents of each element in the array so that each value now lies between -3000 and 3000 inclusive
4. Traverse the array to compute the average value of all elements
I have never worked with arrays before and am lost!
View 2 Replies
View Related
Mar 26, 2013
So I'm trying to make an employee list that holds the information so I can then delete or add from that array. I'm getting some errors regarding overloading function.
#include "employeelist.h"
const Employee & employeelist::operator [](int index) const {
return ( (this->Employees)[index] );
} Employee & employeelist::operator [](int index) {
return ( (this->Employees)[index] );
[Code] .....
View 2 Replies
View Related
Aug 22, 2013
I'm trying to make a list that contains other lists. I want to use the <list> library /not my own implementation/. Here is what I do:
list<list<int>> Lists; //I create the main list
list<int> A, B; //Then I create the other lists
//After I fill them with data I add them to the main list
Lists.push_back(A);
Lists.push_back(B);
The problem comes when I try to go through A and B. I make an iterator of the main list:
list<list<int>> Iter = Lists.begin();
Then I need to make an iterator to traverse through A and B. But I can't make it. I do this:
list<int> Iter2 = Iter.begin() //But the compiler says it is not possible
How can I traverse through A and B?
View 3 Replies
View Related
Nov 26, 2014
i am having trouble figuring out inorder traversal in this assignment. I was given this function definition in the homework i have to declare it. The problem i am having is how to use pointer to function to traverse the list.
void CBSTree<NodeType>::InOrderTraversal(void (*fPtr)(const NodeType&)) const;
void CBSTree<NodeType>::InOrder(const CTreeNode<NodeType> *const nodePtr
,void (*fPtr)(const NodeType&)) const;
//nodePtr is a pointer to a NodeType object (this is a recursive function, initially this points to the root).
//fPtr is a pointer to a non-member function that takes a const reference to a NodeType object as input, and
//returns nothing
I know without the pointer to function parameter it will be like this.
void InOrder(CTreeNode *nodePtr)
{
if(nodePtr != NULL)
{
InOrder(nodePtr->left);
cout<<nodePtr->value;
InOrder(nodePtr->right);
}
[Code]...
I just don't know how to traverse inorder with the function definition i am given.
View 3 Replies
View Related
Jul 15, 2013
I am currently working on a Huffman code program for one of my classes. I have gone all the way up to the point where I have created a tree but when I try to traverse it, the program crashes and says there is a segmentation fault. After running the debugger, the error occurs when I try to traverse to the left node (Line 170). The code I have below is not completely finished, I ran across the issue while testing to see that it works. See the code below:
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <vector>
#include "Node.h"
using namespace std;
void fillList(char* sent, int size, vector<Node> &vec);
[code]....
View 6 Replies
View Related
Jan 10, 2015
I can not seem to add a vector to my head file. I have tried many things and can't figure it out.
Compile and Compile error -
g++ -Wextra -pedantic -std=c++11 Card.cpp Card.h Deck.h Deck.cpp unit_test1.cpp ;
Deck.h:15:27: error: invalid declarator before ‘deck’
void std::vector<Card *> deck;
[Code]....
View 2 Replies
View Related
Mar 20, 2013
Lets say we have a class that holds a pointer member to another object. If I delete that pointer in the destructor I get an error (and I understand why). My question is : is it possible to overcome that without memory leaks ?
1 #include<iostream>
2 using namespace std;
3
4 class A {
5 public:
6 ~A() {
7 cout<< "~A()" <<endl;
[Code] ....
View 5 Replies
View Related
May 16, 2013
In this program I am attempting to allow a user to input three different authors and then input three books they have written as well as the price. I am struggling with calling the functions and am not sure what to do.
#include <iostream>
#include <string>
using namespace std;
struct BookInfo{
string bookTitle;
double price;
[Code] ....
View 3 Replies
View Related
Nov 28, 2014
I have a Point class that's already implemented. My goal is to implement a container class called Line that holds the Point class. I'm not allowed to use any existing container classes for this. Here's what I'm working with:
//File: Point.h
#ifndef POINT_H_
#define POINT_H_
class Point {
public:
Point(int x = 0, int y = 0);
Point(const Point & t);
virtual ~Point() {};
[Code] ....
How I'm supposed to write Line.cpp...how do I access/add Points to Line without using something like a vector? I probably should've included what I've written so far.
#include "Line.h"
/**
* Default Line constructor
*/
Line::Line() {
[Code] ....
View 6 Replies
View Related
Apr 18, 2014
Find all the prime numbers between a given pair of numbers. Numbers should be read in from an input file called "numbers.txt" and find all the prime numbers between them. Store the prime numbers in an array, then sort the array from greatest to least. Display the array before and after the sort.
I'm stuck on how to put the prime numbers into an array.
The input file has the numbers 1 & 100.
Here's what I have so far.
#include <iostream>
#include <fstream>
using namespace std;
int main() {
ifstream fin;
fin.open("numbers.txt");
[Code] .....
View 1 Replies
View Related
Dec 7, 2013
Question: How to find a duplicate numbers and numbers found once in array.
View 7 Replies
View Related
Sep 21, 2014
The code below will generate combinations of numbers from 1 to 25 in an 15 numbers array. The only filter I've applied is that the sum of all the numbers in the vectors divided by 15 needs to be between 13 and 14.
I would like to count how many consecutive numbers there are in one combination, so that later i can apply another filter.. for example:
1 3 4 5 6 8 10 13 14 16 17 18 19 20 25
3 + 4 = 1
4 + 5 = 1
5 + 6 = 1
13 + 14 = 1
16 + 17 = 1
17 + 18 = 1
18 + 19 = 1
19 + 20 = 1
_____________
Count = 8, in this case..
I think it's not very difficult to do, but i just can't see how to do it.
#include <iostream>
#include <vector>
#include <numeric>
[Code]....
View 3 Replies
View Related
May 17, 2014
#include <iostream>
#include<fstream>
int decryption(int);
int multiply(int,int[][2]);
using namespace std;
main(){
int n;
ifstream inFile;
inFile.open ("out.txt");
[Code] .....
I was trying to store numbers read from a text file into 2D array but I am getting the error above.here is where the error occurs:
line 33: cout<<m[i][j]<<" ";
View 4 Replies
View Related
Jan 22, 2015
iam trying to count the same numbers in an array just once like
38
38
40
38
40
37
the output should be 2 since 38 is repeated and 40 too but for my code the output is 3 thats an example of how it should be in a nutshell i want same number to be counted just once in the whole array
and here's my code :
#include <iostream>
using namespace std;
int main(){
[Code].....
View 11 Replies
View Related
Aug 22, 2014
I am unable to create an array which has to content elements(1E, 2E, 3E, 4E). I tried using char however I am unable to display as it only shows the letter E.
View 19 Replies
View Related
Aug 25, 2013
I wrote this GCD function to find gcd of an array of numbers . Here is the code:
long long GCD(long long min,int loc,long long b[]) {
for (long long i=min;i>0;i--) {
bool p=0;
for (long long x=0;x<loc;x++) {
if (b[x]%i!=0)
[Code] ....
Its returning wrong answers. When i tried 6 14 it returns 6
View 6 Replies
View Related
Jul 5, 2014
I was trying to make a program that asks the number of grades, get the grades and then get the average. I know I have to save the grades in an array but i don't know exactly how.
View 12 Replies
View Related
Feb 15, 2014
So i have this program that takes in user input and stores them into an array and then prints them, removes duplicates, and sorts in ascending order. The user can also stop by inputting a sentinel value (in this case -1). But i am also supposed to ignore any negative value besides -1. When i input any other negative value into the program it messes up. How would i go about ignoring the negative values?
Code:
#include<stdio.h>
int main()
{
int input, nums[20], i, j, k, temp, count=0, count2=0;
for(i=0;i<20;i++)
[Code] .....
View 8 Replies
View Related
Aug 19, 2013
I wrote some code for class to find prime numbers.The teacher says that I need to revise my code with the requirement below: use a bit array to store the prime number checking. The bit array will also be in the heap. Use the max value of an unsigned 32-bt integer (UINT_MAX) to be the maximum size of the prime number you want to check.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
#include <getopt.h>
#include <ctype.h>
#include <stdint.h>
//function to set all non-primes to 0
void *zero_multiples(void *threadid);
[Code] .....
View 4 Replies
View Related
Jan 11, 2015
i cannot print the values stored in the array ?
Code:
#include<stdio.h>
#include<math.h>
#include<time.h>
#include<stdlib.h>
double randf();
[Code] ....
View 7 Replies
View Related
Nov 3, 2014
I have task to make program.. To enter 10 positive numbers in an array. Find three numbers that stand near each other and that have the maximum sum.
I understand how to make array, enter 10 number i find only one biggest number from 10.. How to find three max number sum that stand near each other..
View 10 Replies
View Related
Dec 9, 2014
I want to have a program that will calculate the sum of the fourth powers of 100 real numbers in an array (numbers in this array are stated explicitly in the code), code I have for integers is working perfectly but i want it to work also for non integers.
Code:
#include "stdafx.h"
#include "math.h"
int sum_array(int a[], int numelements) {
long double i, sum = 0;
for (i = 0; i<numelements; i++) {
sum = sum + pow((float)a[i], (int)4);
[code]....
The error messages i have: return' : conversion from 'long double' to 'int', possible loss of data.
View 6 Replies
View Related
Dec 10, 2014
I want to write a programm that will reverse the order of the numbers in an array (e.g. as an input 1,2,3.5,4 and as an output i want 4,3.5,2,1) . But i have to problems:
1) I do not know how to properly change the type of a numbers inside an array from int to float or double, changing int to float in front of the a[10], does not change anything instead the code does not want to compile then.
2) I also want to make an array of a number of elements typed by a user with use of a "do" loop and how to put this parameter in the code.
Here is my code:
Code:
// ConsoleApplication3.cpp : Defines the entry point for the console application.//
#include "stdafx.h"
int _tmain(int argc, _TCHAR* argv[]) {
int a[10], i, n;
int pom;
do {
printf("number of elements in an array a: n = ");
[Code]...
View 5 Replies
View Related
Jul 11, 2014
How to assign numbers stored in a buffer memory to a 2D array.
The data type is unsigned 16bit (unsigned short) integers and they are stored in a 16bit/2bytes*1280*1024=2621440 bytes memory. The pointer pBuffer is the starting address of the buffer. Now I initiated an array and then assign the numbers to the array.
Code:
unsigned frame[1280][1024];
for (int i=0;i<1024;i++){
for(int j=0;j<1280;j++){
frame[i][j]=(*(unsigned short*)pBuffer+1024*i+j);
printf("%u ", frame[i][j]);
}
printf("
");
}
Because I know the number in the memory, I know after running the code that the result gives me nonsense.
I tried frame[i][j]=(*(unsigned short*)pBuffer+1024*2*i+2*j);
Since I think the pointer needs to move 2 bytes at a time, but it still gives me nonsensical array back.
Am I using the wrong expression to assign the values?
View 3 Replies
View Related
Jan 30, 2015
So I already gave this a go and will post my code below. The question is about the last loop before the program cout's. i thought the count would keep track of the repeated numbers so i could display them but it does not. I italicized the loop i am referring to. or at least i tried to xD
#include <iostream>
#include <iomanip>
using namespace std;
int main() {
int inputs[30];
int numofloops;
int maxvalue = inputs[0];
[Code]...
View 1 Replies
View Related