C++ :: How To Find The Number Of Objects Without Loop

Nov 25, 2013

how i can find the 5 without loop?

vector<int>i;
vector<int>j;
i.push_back(1);
i.push_back(2);
j.push_back(3);
j.push_back(4);
j.push_back(5);

[Code]...

View 10 Replies


ADVERTISEMENT

C++ :: How To Loop Through A Vector Of Class Objects

Mar 22, 2013

For a beginners C++ lab, I have a base class Employee and two derived classes HourlyEmployee and SalaryEmployee. In main, I have a vector defined as vector <Employee *> VEmp; It's then passed to a function to get the input, which works fine. But what I'm struggling with is another function with the header "printList(const vector <Employee *> & Ve)". It's supposed to loop through the vector and call the appropriate printPay function, which is a seperate print function inside each derived class. How do I loop through the vector and print it out? I was trying to do a for loop and something like "Ve[i].printPay();", but that doesn't work. So how would I do it?

Here's some snippets of the relevant code.

class Employee {
....
virtual void printPay() = 0;
};
class HourlyEmployee : public Employee {

[Code] ....

View 4 Replies View Related

C++ :: Loop Through A Vector Of Objects Calling A Function In Each

Apr 18, 2013

I am having a problem with a program. I have a bunch of classes all derived from the same base class. I want to loop through a vector of objects, calling a function in each. The problem is that it doesn't matter which class the objects are, only the function defined in the base class is called.

I simplified the code as far as possible to replicate the problem. As you see, I would like a mix of numbers 1,2,3 as the output, however using the vector the only number output is 1. Here is a copy of the output by the way:

base->num() : 1
a->num() : 2
b->num() : 3
(*it)->num() : 1
(*it)->num() : 1
(*it)->num() : 1

I suspect this is the "slice" problem, because the vector is defined with pointers to the base class so it uses the base class functions? The question is how to get around it? How can I loop through a vector of objects sharing the same base class but calling each by their correct member functions?

Code:
#include <iostream>
#include <vector>
class Base {
public:
int num() { return 1;}

[Code] .....

View 5 Replies View Related

C++ :: Find Next Palindrome Number Larger Than Input Number

Nov 14, 2014

Q. WAP to find the next palindrome number larger than the input number.

for eg:-

Input=25
Output=33

The program is giving correct output for number with all digits '9';

why is it not giving output.

#include<iostream>
#include<string>
using namespace std;
int main() {
int len,flag=1,count=0,num,ind;

[code]....

View 8 Replies View Related

C/C++ :: Find Number Of Comparison And Number Of Exchanges

Apr 20, 2015

I want to find number of comparison and number of exchanges from a list or other than a list. My list is this

12 5 11 19 4 7 8 10 9 6 22 2 9 1 32

First, I am looking for number of comparisons from a list of 10 numbers with 21inversions made by selection sort, insertion sort, exchange sort.

Also, I like to find number of exchanges from a list of 10 numbers with 21inversions made by selection sort, insertion sort, exchange sort.

View 6 Replies View Related

C/C++ :: Program To Find Number Of Digits In Number?

Aug 20, 2014

In the c pgm to find number of digits , if I am giving 001 as the input number ,why I am not getting the no. of digits as 3?

View 2 Replies View Related

C++ :: Where To Define A Large Number Of Objects

Jan 20, 2013

I am trying to run a simulation with a large number of objects (mainly arrays and vectors). I am not sure where shall I define my objects: inside or outside of the main() function, like the following two structures:

(1) ---------------

//main.cpp
int main(){
array<double, 1000> a_1 = {};
array<double, 1000> a_2 = {};
......
func_1(a_1, a_2, ..., a_100);
return 0;
}

[Code]...

I know there is a question about scope. But besides this question (which seems have no difference between these two structures here), is there any difference in terms of execution performance or security issue?

View 3 Replies View Related

C++ :: Limit In Number Of Objects Of Class?

Jul 8, 2013

Is dere is any limit in number of objects of a class?

View 16 Replies View Related

C/C++ :: How To Find Average With For Loop

Sep 14, 2014

What i need it to do is ask the user for a number of cases. The user will input numbers and the program should add the inputs until zero or a negative number is entered and then out put the average of those inputs. The amount of cases is pretty much how many times an average will be done. so if the amount of cases is 4. and the inputs are 1,3,(1+3/2)0 then it should output 2. and that would be ONE case, next inputs are 5,6,4,0(5+6+4/3) the output is 5 and that is case two. etc.

Here is my code:

#include <iostream>
using namespace std;
double avgVal(int, int);
int main() {
int amountOfCases;
cin >> amountOfCases;
int* numbers = new int[amountOfCases];
int input=0;

[Code] ....

View 2 Replies View Related

C++ :: For Loop To Find Vector Length

Jul 21, 2013

Ok my assignment has me doing vector math with some canned code provided for me by the instructor This is the header file to the class I'm working with and the .cpp file as far as I've gotten it.

#pragma once
#include "Scalar.h"
class Vector2D {
public:

Vector2D();
Vector2D( const Vector2D& ) ;// copy constructor
Vector2D( Scalar element[2] ) ; // initialize with an array

[Code] ....

I'm having trouble seeing which data members I'm multiplying together and what the initial state, continuing state, and after loop action I'm supposed to be using in the for loop.

View 1 Replies View Related

C++ :: Getting Multiple Objects To Move Randomly At A Designated Number Of Intervals

Jan 9, 2015

I have a piece of code which gets multiple objects to move randomly at a designated number of intervals. I want to create a function which calculates the distance between each pair of points after that number of steps. so the distance between object 1 and object 2, object 1 and object 3, ..., object 1 and object N. then object 2 and object 3, object 2 and object 4, ..., object 2 and object N. then then object 3 and object 4, object 3 and object 5, ..., object 3 and object N and so on until the distance between all the pairs of points have been calculated.

#include <ctime>
#include <cstdlib>
#include<iostream>
#include<cmath>
#include<iomanip>
#include<fstream>
#include<vector>

using namespace std;

double dist(int a, int b);
int X(int x1, int x2);
int Y(int y1, int y1);

[Code] ....

View 4 Replies View Related

C++ :: For Loop To Find Index Values - Reference List

Mar 18, 2013

im using a for loop to find the index values of the tied high scores and store them into string list then reference list in the second for loop to output it to screen however it isnt letting me use an array with index i as an index its self.

void printHighest(Student s[], int length){
int index;
string list[10];//you're never going to have more than 10 people with a tieing highscore.
index = findMax(s, length);

[Code] ....

For the time being I simply removed the idea of string list and just put the contents of the second for loop into the if statement above it. However, I am still curious as to if I can reference an index of an array in an index of another array.

View 1 Replies View Related

C/C++ :: Find Max Number Entered By User And Terminate When Negative Number Entered

Jul 15, 2014

I am trying to find the max number entered by the user, and it should terminate when a negative number is entered. For my code, it will just end when the user inputs a lower number than the previous. i.e.- 10 20 15 "The highest number is 20" when it should be "10 20 5 40 15 -1" "The highest number is 40". No arrays or do/while loops either.

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

[Code] ....

View 9 Replies View Related

C :: How To Find First Bad Number From Float

Mar 6, 2015

I am entering numbers to float ... I want program to find out, which first number is not from specific interval. How to do it ? Example: Enter input : 5 10 20 30 50 46 . 30 is invalid. Here is the code :

Code:

while(scanf("%f",&input)!=EOF || input==0) {
sum=input+sum;
if (getchar() == '

[Code]....

View 3 Replies View Related

C/C++ :: Find Biggest Number In A Row

Apr 11, 2015

I'm having this issue where my else if statements are not working. My program only works correctly when I go into my if statement and change the first part of my array to the specific row I am trying to find ex [0][i] to [1][i].

int getMonthMax(int scores[][COLS],int month){
int highestN=0,i, j;
for (i=0;i<ROWS-1;i++){
if(scores[0][i]>highestN){
highestN=scores[0][i];

[Code] .....

View 4 Replies View Related

C :: How To Delete Number And Find Closest

Aug 3, 2014

Write a function which takes string of integer numbers and length of a string. Function need to return value of element which is close to mean of all numbers and transform string without that element. If are more numbers in same "distance" of mean, function need to find first and throw that number-element.

In function without []..
with pointers.

I wrote this code below, programm find mean of elements and if mean is equal to any element of string.. the programm return that number. Idk how to delete that number and how to find closest.. for ex:

String=[4,7,10,3]
sum=24
mean=6

The progoramm need to delete 7 and new string look like String=[4,10,3]

insert Code:
#include <stdio.h>
int main()
{
int n,i;
printf("Elements of string ?

[Code]....

View 9 Replies View Related

C :: Find Largest Number Of 3X3 Matrix

Jul 26, 2013

Its a code to find the largest number of a 3X3 matrix.The logic seems to be right........

Code:

#include<stdio.h>
main() {
int matrix[3][3],i,k,j,*a;
a=&matrix[0][0];
printf("Enter the elements");
for(i=0;i<=2;i++) {

[Code]....

View 7 Replies View Related

C++ :: Find Smallest Number In 2D Array

Oct 21, 2014

I need to generate a grid of 10x8 and fill it with random numbers (max 70), then i need to find the smallest number within the random numbers generated and my "findSmallest" function does not seem to work and i do not know how to make it work...

Here my Code:

include <iostream>
using namespace std;
int main() {
int row=0;
int col=0;
int ArrayGrid [9][11];
srand(time(NULL));

[Code] .....

View 4 Replies View Related

C++ :: How To Find The Smallest Number Amongst 4 Numbers

Nov 28, 2013

How to find the smallest number amongst 4 numbers

View 10 Replies View Related

C/C++ :: Find Second Smallest Number Out Of 4 Numbers

Oct 23, 2014

I'm trying to find the second smallest number out of 4 numbers. I produced some code that does that but it doesn't work if i have duplicate numbers.

secondSmallest = a;
if(b > secondSmallest) {
secondSmallest = b;
}
if(c < secondSmallest) {

[Code] ....

View 1 Replies View Related

C/C++ :: How To Find Smallest And Largest Number

Oct 18, 2014

Write a program that will find the smallest, largest and average of the values in a collection of N positive integer numbers.

View 12 Replies View Related

C :: Logic To Find Least Common Number Combination

Nov 22, 2013

I have been struggling with this program. I am somewhat new to c and suck at logic. I have a personal program I want to make that I will try to get extra credit for in school. I have a printed set of winning lottery numbers form the last 10 years. I chose the easiest one do do logically which is just 5 numbers none repeating.

I am trying to find out how I can print the least common 10 sets. I think if there are any set which have not been picked I would have to print all of those because logically they would all be equal, then print sequentially the sets least picked up to 10.

I have pseudocode which I am sure is wrong but will post it just to show that I am trying. My first attempt was to add the numbers but quickly realized that that wouldn't work ...

5 Nums Pseudocode
Code:
Read Nums
Parse Into Ints
Make Array [185] //39+38+37+36+35 The highest the numbers added together can go

//LOGIC

[Code] ....

View 5 Replies View Related

C :: Function To Generate A Number - How To Find Same Digits

Nov 15, 2014

I use rand function to generate a number which consists of 3-5 digits(e.134,1435,73463..). The user decides whether he wants a 3 digit,4 digit or 5 digit number.After that,the user tries to guess the number.Its like mastermind game.The user will enter a number (with the same amount of digits) and the program will calculate how many digits from the secret number he has found and also how many digits he has found in the correct position(e.if the generatir produces the number 32541 and the user tries the number 49581 the program should tell him that he found 3 digits (5,1,4) and 2 digits in the correct position(5,1)) so that after some tries he finds the secret number.My problem is with the functions so that i can compare the digit of each number,find the amount of same digits and the amount of digits in same position.

View 5 Replies View Related

C :: How To Find Biggest Prime Factor Of A Number

Feb 10, 2015

i want to write a program which find the biggest prime factor of a number for example the biggest prime factor of six is three or the biggest prime factor of fifteen is five. What is my program bug

Code:

#include <stdio.h> // main functions
#include <math.h> // for sqrt function
int main()
{
int i, j, k, f; // F = Flag;
printf("Enter K

[Code]...

View 9 Replies View Related

C :: Program To Find The Square Root Of A Number

Dec 5, 2013

I am writing a program to find the square root of a number. I am using the Newton-Raphson method..Just to clarify a bit of the code.. fabs(1/(x+1)) < 0.001 is to check for relative error..

EXAMPLE: for user entry of 100 if the iteration process ends in 10.055 the answer will return as 10 which is correct. But it isn't doing that.

It compiles I then proceed to run it..prompts me "Enter a number to find the square root of: I type 100 then hit enter...

"The square root of 100 is -1077834936"

My first time writing a program from complete scratch.

And I know there is a sqrt() function...just wanted to write my own.

Code:

#include <stdio.h>
#include <math.h>
double mysqrt(double a);
int main()
{
double a, result;
printf("Enter a number to find the square root of: ");

[Code]...

View 1 Replies View Related

C++ :: Find Largest Prime Number K Of Array 1

Aug 30, 2013

While finding the primes , I do not know how to make it into one array so that...

View 7 Replies View Related







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