C++ :: Program To Create A Vector Of Integers

Feb 10, 2015

1. Write a c++ program to create a vector of integers. copy the vector contents into list, sort the contents, then copy selected items into another vector(like elements less than 10 etc)

View 1 Replies


ADVERTISEMENT

C++ :: Recursive Function 2 - Create Vector Of Vector Of Integers

Mar 26, 2013

Lets say that I have a vector of vector of integers. <1,2,3,4> , <5,6,7,8>, <10,11,12,13>

How do I make a function that creates vector of vector of every different integers?

<1,5,10> , <1,5,11>, <1,5,12>, <1,5,13>
<1,6,10> , <1,6,11>, <1,6,12>, <1,6,13>
<1,7,10> , <1,7,11>, <1,7,12>, <1,7,13>
<1,8,10>, <1,8,11>, <1,8,12>, <1,8, 13>
<2,5,10>, <2,5,11>, <2,5,12>, <2,5,13>

and so on...

View 2 Replies View Related

C++ :: Create Program That Will Accept 2 Integers?

Sep 4, 2013

how can I create a program that will accept 2 two integers. the user will also choose among the different operations:

1 for addition
2 for subtraction
3 for multiplication
4 for division

View 4 Replies View Related

C++ :: Program To Create Integers As User Requests?

Nov 19, 2013

I know it would be easier to create an int array, but I want the program to create integers as the user requests. For example, if the user enters 4, the program creates 4 integers with the names input. Each integer is better as an individual integer, not an element in an array.I'm not sure if this is a possibility with C++.

View 3 Replies View Related

C :: Create Function To Create A Dynamic Array That Fill With Randomly Generated Integers From 0 To 50

Oct 26, 2013

I have a struct called Array and I'm to create a function to create a dynamic array that's fill with randomly generated integers from 0 to 50 (inclusive) and a function to destroy the array for freeing its memory. Below the code that I have written so far.

Code:

* Struct */
typedef struct {int *pArray; //the dynamic array
int length; //the size of the dynamic array}Array;
/* Function to create a dynamic array */
Array *initializeArray (int length) {int i;
}

[code]....

View 7 Replies View Related

C++ :: Create Class Vector As Template And Define Operations On Vector?

May 13, 2013

I need to create a class vector as a template and define operations on vectors.

And this is what I made.

#include<iostream>
using namespace std;
template<class T>

[Code].....

View 2 Replies View Related

C++ :: Vector With Strings And Integers

Apr 5, 2013

I have a file where the first column shows letters, second column shows numbers.

How could I make it to print everything exactly how it is in the file – a vector/array with strings and integers?

View 13 Replies View Related

C++ :: How To Make 5x3 2D Vector Of Integers

Feb 8, 2014

I am trying to make a 5x3 2D-vector of integers, then set its i-capacity to be 5 and j-capacity to be 3, i.e:

vec2D[i][j] i = 1,2,3,4,5 j = 1,2,3

and then assign integer values to it.

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

[Code] ....

It compiles, but does not work properly:

Test.exe exited with code -1073741819

i-capacity before reserve: 0
i-capacity after reserve: 5

i = 0
j-capacity before reserve: 336043326
j-capacity after reserve: 336043326
i = 1
j-capacity before reserve: 4282929217
j-capacity after reserve: 4282929217
Press <RETURN> to close this window...

I am trying to convert a C code with dynamic 2D arrays, to a C++ code. I prefer to keep the vec2D[i][j] = ... way of assignment instead of using vec2D.push_back(...).

View 8 Replies View Related

C++ :: Vector Of Integers - How To Print Indicators

Apr 8, 2014

I have a vector of integers, which can take values 0 or 1, say

0 0 0 1 1

I need to print indicators, telling in which position 1-values occur. So

0 0 0 1 0
and
0 0 0 0 1

View 11 Replies View Related

C++ :: Create Single Integer Number From Four Integers

Mar 1, 2014

Write a program that reads four integers from a file ‘input.txt’.

The program will then create a single integer number from the four integers. The output of the program will be the single number and single number + 600.

#include <iostream>
using namespace std;
int main () {
int number , a;
cout<<"enter en integer number";
cin>>number;
cout<<"add 600 to the number"
cin>>a=number+600;
return 0;
}

View 5 Replies View Related

C :: Binary Tree - Return A Vector Of Integers

Oct 19, 2014

So I have a Binary Tree and I need to return a vector of integers (the nodes) of the heaviest path in the tree.

First, is it possible to do in C? Because I think a vector is an ADT in C++.

I've started writing something recursive, which worked for a balanced tree of height 1, and failed for longer height.

This is what I've written - [URL] ....

View 6 Replies View Related

C++ :: Create A File And Fill 6x6 Array With Random Positive Integers

Nov 24, 2014

This is my code!! but it's not working at all.

#include <iostream>
#include <fstream>
#include <cstdlib>
#include <ctime>
using namespace std;
int main () {
ofstream fout("datain.txt",ios::out);
int array[6][6];

[Code] .....

View 5 Replies View Related

C++ :: Generate Random Integers In The Range And Store Them In A Vector

Sep 3, 2014

You are to write a C++ program to generate random integers in the range [ LOW = 1, HIGH = 10000 ] and to store them in a vector < int > of size VEC_SIZE = 250. Then, sort the contents of the vector (in ascending order) and display it on stdout.

To sort the contents of a vector, use the sort ( ) function from the STL. In addition to the main ( ) routine, implement the following subroutines in your program:

• void genRndNums ( vector < int >& v ) : This routine generates VEC_SIZE integers and puts them in vector v. Initializes the random number generator (RNG) by calling the function srand ( ) with the seed value SEED = 1, and generates random integers by calling the function rand ( ).

• void printVec ( const vector < int >& v ) : This routine displays the contents of vector v on stdout, printing exactly NO_ITEMS = 12 numbers on a single line, except perhaps the last line. The sorted numbers need to be properly aligned on the output. For each printed number, allocate ITEM_W = 5 spaces on stdout.

Programming Notes:

• You are not allowed to use any I/O functions from the C library, such as scanf or printf. Instead, use the I/O functions from the C++ library, such as cin or cout.
• Let v be a vector of integers, then the call: sort ( v.begin ( ), v.end ( ) ) sorts the elements of v in ascending order. The detailed description of the sort ( ) routine can be found on the course web site and in the course textbook.
• Execute the srand ( ) function only once before generating the first random integer with the given seed value SEED. The rand ( ) function generates a random integer in the range [ 0, RAND_MAX ], where the constant value RAND_MAX is the largest random integer returned by the rand ( ) function and its value is system dependent. To normalize the return value to a value in the range [ LOW, HIGH ], execute: rand ( ) % ( HIGH – LOW + 1 ) + LOW.

View 1 Replies View Related

C++ :: Sorting Large Numbers Of Vector Of Random Integers Returns All Zero

Jul 31, 2014

I tried to sort a large numbers of vector of random integers with std::sort(), but when the number increases over 10M, std::sort returns all zero in values. Does std::sort have a limitation of input numbers?

View 1 Replies View Related

C/C++ :: Statistical Analysis Of Vector Data Set Of Integers - Frequency Distribution?

Feb 16, 2014

I am working on my second c++ project that is a statistical analysis of a vector data set of integers. I created a struct called range with a maximum value, minimum value and count for occurrence. The frequencies are stored in a vector<range>.

I have tried messing around with the increment to get the high range test above the maximum element, though my ranges cumulatively increase by 9.9. I'm not sure how the professor's program has the distance between high and low ranges as 10 instead of 9.9, which is the increment. I'm also unsure why half way through his buckets, the ranges appear as 9.99 briefly and then return to 10 after (40.00 - 49.99 then 49.99 - 59.99). I feel like I am missing something very obvious on line 04 or 10 in my code.

Here is the code for the frequency that accepts a vector reference for my data set of integers.

void frequency(vector<int>& data_set){
double max = findMax(data_set);
double min = findMin(data_set);
double increment = (max - min) / 10;
double percentage = 0.0;

[Code] ....

View 1 Replies View Related

C/C++ :: How To Create A Vector In Header File That CPP Object Can Use

Apr 25, 2015

I'm working on a grocery store inventory project. One part is to have a shopping cart, where customers can put in up to 20 items. Because there can be up to 20 shopping carts at one time, I want to use a vector inside the cart object to represent all the individual food items.

Here's my code,

Header:

#ifndef CART_H
#define CART_H
#include <vector>
class Cart {
public:
Cart();
Cart(std::vector< int >, std::vector< int >)

[Code] ....

View 9 Replies View Related

C++ :: Create A Vector Using A Structure Definition As The Basis?

Jan 25, 2013

I was trying to apply what is here (as someone who writes rarely and has to relearn everything each time): [URL] ....

I'm using a header file to define the structure:

#ifndef EINSTEIN_H
#define EINSTEIN_H
#include <stdio.h>
#include <vector>
struct SizeAll{
double year;
double R;
SizeAll() { year = 0; R = 0; }

[Code] ...

This creates quite a mess. It seems that somehow the "vector" declaration isn't working as the referenced web link seems to suggest that it should. I presume that, as usual, clearing one real error will eliminate the cascade of errors the first real error produces. Why won't VC++ accept the "vector" identifier?

The error messages that follow the build attempt are:

Friedman.cpp
d:documents and settingsxxmy documentsvisual studio 2010projectsfriedmanfriedmanEinstein.h(22):
warning C4996: 'fopen': This function or variable may be unsafe.
Consider using fopen_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS.

[Code] .....

View 14 Replies View Related

C :: Program Using A Function To Add Two Integers

Dec 2, 2014

I compiled a simple program using a function to add two integers, in order to understand the difference of calling by value vs by reference.

Code:

#include <stdio.h>
int add(int a, int b, int sum);
int main()
{
int a, b, sum;
printf("Please enter two integers
");
scanf("%d%d", &a, &b);

[Code]...

I used these two (one, two) pages I found in the net while studying the subject. So here are my questions...

(a) Do I understand correctly that in the above code all a, b and sum are passed by value?
(b) If I wanted only sum to be passed by reference, how the above code would change?
(c) If I wanted all (a, b, sum) to be passed by reference, how the above code would change?

View 12 Replies View Related

C :: Program To Write Even And Odd Integers Into Different Files

Sep 7, 2014

-Create three files namely all_numbers, odd_number and even_number.
-Get the number of records and the numbers to store from the user.
-Save all the numbers in "all_numbers" file.
-Read each number and check whether it is odd or even.
-Store the odd numbers in odd_number file and even numbers in even_number file.
-Finally, print the numbers stored in odd_number and even_number files.

The output of the program will look like this.

How many records that you want to store :41

2
3
4

The data are written too the respective files.

The even numbers are 2 4
The odd numbers are 1 3

View 9 Replies View Related

C :: Program With Integers And A Text File

Oct 6, 2014

I created a program in C that opens a text file and read integers.

File `numbers.txt`:
> 5 6 4 6 3 4 1

I use some restrictions with the `fscanf()` library and read the file, just for the record, and if everything goes fine I proceed to the next part of code (I know I could use `fget()`, but this is how I done it).The next part of the code will be this:

- First, I want the program to read the numbers from right to left. I look for an answer in google and many C courses, but I couldn't find a working solution. I think that the code is pretty simple, though I can't find it. **To be clear for the next part, we read the numbers from right to left.**

- Then, this is the hard part for me: I want the program to read the numbers and if the next number is higher (**not equal**) than the last one to keep it in memory, **only if it's higher than the last.** This can be continue for **1 through 500.000 numbers.**

- Then when the procedure done, I want to print how many numbers are greater than the last one and save it to a file, which I can do it if I solve the last problem.

We have the below numbers:
5 6 4 6 3 4 1

The program starts to read the numbers from right to left.Now which and how many numbers are greater than the last one?

The answer is 3 numbers:
5 6 4 [6] 3 [4] [1]

Then the program will print the number 3 with a message and save it to a file called `xxx.txt`. I am posting the `main` code, not all the code just what you need to solve the problem:

Code:

#include <errno.h> // macros for reporting and retrieving error conditions
#include <stdio.h> // load main C library function
#include <stdlib.h> // need that for many reasons ;)
#include <string.h> // loads various of characters functions
}

[code]....

In conclusion, I want 2 pieces of code:

- One to read the numbers from right to left
- One to make the above procedure

View 11 Replies View Related

C++ :: Possible To Use Extremely Large Integers In Program?

Mar 23, 2013

Would it be possible (and how) to use extremely large integers in a C++ program (such as 20 million digits)?

View 7 Replies View Related

C++ :: Add Program That Declares Two Arrays Of 5 Integers Each?

Dec 9, 2014

Add a program that declares two arrays of 5 integers each. Provide one function to load one array with random numbers from 1 to 25, another to fill the other array with user supplied values, and a third function to print an array. The main function should declare the arrays and use the functions to fill each array and print them both.

The code i have so far is
#include <cstdlib>
#include <iostream>
using namespace std;
void printaaray(int printarray1[], int printarray2);

[Code]....

View 2 Replies View Related

C++ ::  Write A Program That Prints Out The Even Integers Between 2 And 100

Nov 4, 2013

#include <iostream>
using namespace std;
int main()
{
int num = 2;
int count = 0;
while (num <= 100)

[Code]...

The code works fine but I need the results to print out like

2 4 6 8 10 12 14 16 18 20
22 24 26 28 30 32 34 36 38 40

instead of

2
4
6
8
10
12

I don't know how to do this.

View 7 Replies View Related

C++ :: Program Producing Unwanted Integers?

Jan 17, 2012

I have done some programming or rather have written and implemented some algorithms in code (is there a difference? :-)) but am having some teething problems with a project I am currently working on.

To start with I am simply reading in the data from a .txt file into a vector. This is the code that I currently have:

Code:
#include <iostream>
#include <fstream>
#include <cerrno>
#include <vector>

[Code]....

This compiles fine but currently outputs a different integer everytime I run the program. I can remember having this problem when I started coding before using a different language and if I remember correctly it was quite easy to resolve. I thought it was because I had missed off the 'return 0' but it is something similar I think.

View 3 Replies View Related

C++ :: Passing Stings And Integers From A File To Program

Oct 18, 2014

I want to pass some strings and int values from a file to my program.. if i was using c i could do it like

scanf(p,"%s %s %d %d",&str1,&str2,&num1,&num2);

I don't want to get the whole line just the values one by one if i wanted the whole line i could do it with getline(); now that i want the values one by one how do i do it ...

View 7 Replies View Related

C/C++ :: Program That Keeps Reading Integers Until User Enters -1

Nov 21, 2014

This is the problem :- Write a program that keeps reading integers until user enters -1, then it prints the maximum and the minimum among all numbers (-1 should be ignored).

View 14 Replies View Related







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