C++ :: Counting Integers With Array?

Mar 21, 2014

I now know how to count integers with while loop but I'm not sure how to count the integers with array.

So the question is:

1. program should keep reading integers as long as the integers are within [0,9999]
2. when user typed the integer not between 0 to 9999, the program print out the numbers of integers that were typed.

Sample
3
3
3
9999
9999
-1
You entered 3 3 times.
You entered 9999 2 times.

#include <iostream>
using namespace std;
int main() {
int i=-1;
int x;
int numbers[10000];

[Code] ....

I cannot use "do" ....

View 1 Replies


ADVERTISEMENT

C++ :: Counting Occurrence Of Char In Array

Aug 25, 2013

I have

#define LessSymbol -1
#define MoreSymbol 1
#define NeitherSymbol 0
#define MAX_COLS 20
#define MAX_ROWS 20
#define MAX_CHAR 3

char averageMap[MAX_ROWS][MAX_COLS];
char rowCount[MAX_ROWS][MAX_CHAR];
char colCount[MAX_COLS][MAX_CHAR];

I have other stuff in the code, but basicly, at this stage i have a map of 1's,-1's and 0's in a 20 x 20 map. I just want to count the occurrences of each and basicly tally them in the 3 colums/rows at the end of the bloc. so far ive tried many different if statements, here is what i tried last.

for(row = 0; row < MAX_ROWS; row++){
rowCount[row][0]=0;
rowCount[row][1]=0;
rowCount[row][2]=0;
} for(col = 0; col < MAX_COLS; col++){

[Code] ....

Its outputting 20 as a result for each row. Its like its counting the symbol no matter what it is.

View 2 Replies View Related

C++ :: Counting Elements Of Constant Array?

Aug 21, 2014

This is going to seem like a stupid question but how can I count the elements of a const array. Surely c++ compilers provide a count for const arrays?

const wchar_t* ItemHandler::itemNames[] = {L"Coins", L"Matches"};
const wchar_t* ItemHandler::itemIconLocations[] = {L"./media/Items/coins.png" , L"./media/Items/matches.png"};
const bool ItemHandler::itemStackable[] = {1, 1};

I want to count the elements so if the server sends a bad item id it won't crash every client in range lol. I heard that the sizeof keyword returns the size of the array in bytes. I used to think the size of keyword would return the element count but found out it isn't.

View 2 Replies View Related

C/C++ :: Counting Letters In Char Array

Dec 7, 2014

Assume input is char array holding a C-string. Write code that counts the number of elements in the array that contain an alphabetic character.

counter = 0
for (i = 0; i < SIZE; i++) //size being whatever input's size is
{
if (isalpha(input[i]))
{
counter++;
}
}

Is there anything wrong with this method?

View 8 Replies View Related

C++ :: Generic Class Array Based On Counting Access To Elements

Oct 3, 2013

I was writing generic class Array (based on counting access to elements)and i got compiling error I cannot even understand (in vs2012).

Code: [URL] ....
Error: [URL] ....

View 8 Replies View Related

C++ :: Assigning Elements Of 1-D Integer Array Into 2-D Array Of Integers

Sep 27, 2013

I am having problem in writing the code for the problem "To assign the elements of 1-D integer array into 2-D array of integers such as if the array is 1,2,3,4,5,6 The resultant 2-D array should be like :

1 0 0 0 0 0
1 2 0 0 0 0
1 2 3 0 0 0
1 2 3 4 0 0
1 2 3 4 5 0
1 2 3 4 5 6
"

View 7 Replies View Related

C# :: Take Sorted Array Of Integers And Return Array Compacted

Jun 7, 2014

Objective: Write a function with the given signature that will take a sorted array of integers and return the array compacted. That is, given an array containing: 1, 2, 6, 8, 8, 8, 9, 10, 10, when the function returns, the contents of the array should be: 1, 2, 6, 8, 9, 10.

Restrictions:
Cannot use Distinct method

Signature:
public static int[] CompactArray(int[] input)

View 14 Replies View Related

C :: Sorting Array Of Integers Between Two Positions Of Array

Apr 18, 2013

I am trying to create a code to sort an array of integer, but only between two positions of the array, not the all array.

like this:

array: 1 2 5 4 7 2 9 8
index: 0 1 2 3 4 5 6 7

i want to sort the array per exemple between the the index 2 and 5.the result is... array: 1 2 2 4 5 7 9 8

View 2 Replies View Related

C++ :: Using Array Of Integers In Classes

Feb 28, 2013

How to solve this because i was not able to get the idea of using arrays in classes . Here is the Question :

The PVR Cinemas manager approaches you to develop a system for ticket booking. It has 5 screens each with a capacity of 500 seats of which 100 are platinum, 100 are diamond and 300 are gold. The cost of platinum, diamond and gold tickets are Rs.150, Rs.125, and Rs.100 respectively.

•Construct a class to model a screen with an array of integers for each category of seats. Provide a constructor to initialize the array to 0.
•Include a member function bookSeat() that gets the category of the seat and number of tickets to be booked and then prints the seat numbers. The booked seats are marked by 1. If no seats are available then your program should display appropriate message.

Test the above class with a main program that creates an array of objects (size of array depends on the number of screens) and display the total amount to be paid by the visitor.

View 6 Replies View Related

C :: Storing Integers To Character Array?

Jan 30, 2013

I am creating a program and am having trouble with one part. The input file looks like:

Code:
2
3
Bill Jeff Steve
Linda Brittany Jessica
3 1 4
2 1 9
8 3 7
6 4 9
4 8 9
6 9 4

Where I am stuck is putting the numbers into the string array. For example, Bill's scores should be set up as Bill's scores should be set as

Code: bill_score = {3, 1, 4}

and when this code is ran

Code:
for (i=0; i<3; i++) {
printf("%s - ", men[i]);

[Code].....

View 4 Replies View Related

C++ :: From Array Of Integers Take Numbers That Occur Only Once

Jan 14, 2013

I am trying to find a way to do something like this:

input: 3 4 7 4 3 3 7
output: 3 4 7

So what I am trying to do is from an array of integers to take numbers that occur only once. If 3 is in that away I am trying to input it in a different array only once.

input: 8 3 2 9 8 9 2
output: 2 3 8 9

I cannot find a way to solve this, and I have been trying to solve it for a long time.

View 8 Replies View Related

C++ :: Storing Integers At Odd And Even Indexes In Array

Mar 4, 2013

here is my problem given below Input values (say 10) from user in array, if the value is even then place at even index else at odd index. Then how could i solve this problem?

View 2 Replies View Related

C :: User To Enter 4 Integers Which Get Stored In Array

Mar 3, 2014

For my code, I want the user to enter 4 integers, which get stored in an array. Here are the lines I wrote relevant to this part:

Code:
printf("Enter 4 integers:
");
scanf("%d %d %d %d," &a, &b, &c, &d);
z[4] = { a, b, c, d};

Would this be correct?

View 5 Replies View Related

C :: Initialize Dynamically Allocated Array Of Integers To Zero?

Jun 22, 2013

Suppose I wished to initialize a dynamically allocated array of integers to zero. Would I do better to use calloc() or malloc + iterate over all entries setting each to zero? Which one is regarded as a better approach?

View 12 Replies View Related

C :: How To Take String Of Numbers And Turn It Into Array Of Integers

Mar 2, 2014

I know strings are essentially just arrays of characters, so what would be the easiest way to take each individual digit and put it into a separate space in an array?

ex.) *str = "90210"

array[0] = 9
array[1] = 0
array[2] = 2
array[3] = 1
array[4] = 0

All my attempts at achieving this just result in an array full of garbage numbers. What I've done is

Code:
int *array;
array = malloc(sizeof(int)*(strlen(str));
for(i=0; i<strlen(str); i++) {
array[i] = str[i]
}

also, I should mention that the string will be defined in main, and its converted into an array in a separate function.

View 2 Replies View Related

C :: Finding And Returning A Common Value From Array Of 8 Integers

Feb 23, 2013

how assignments is to write code that returns a value that is repeated more than once in an array of 8 integers.For example:

Numbers: 30 12 100 33 2 2 1 40
Output: 2

Here is my code so far:

Code:

#include <stdio.h
int main(void){
int i;
int j;
int arr[8];
int count;
int maxCount = 0;
int maxValue = 0;

[code]....

When I compile this code (on Vim), I get an error when I print the array of 8 above (see ERROR above).

View 3 Replies View Related

C++ :: Calculate Mode / Mean And Median Of Array Of Integers

Mar 17, 2013

I'm writing a program that calculates the mode, mean and median of an array of integers. I've done the mean and median but i can't figure out mode.

I have to find the number of modes.

Ex array of integers: 10, 10, 4, 5, 6,5, 6, 7

My function has to return the number 3. Because there are 3 sets of mode; 10,5,6.

How do i implement this in my function because for now it just calculates the mode. It only return the number mode 10.

View 5 Replies View Related

C++ :: Display 3rd Element Of Two Dimensional Array Of Integers

Feb 13, 2015

display the 3rd element of a two dimensional array of integers named T of size 2x5?? i don't know how to start because i'm just advance studying with array

View 4 Replies View Related

C++ :: Display Maximum Value In Two Dimensional Array Of Integers

Feb 3, 2014

How will you code a program that displays the maximum value in a two dimensional array of integers. the program will ask the user to input the 4x5 array?

i know how to code it. but i dont know what to do to find the maximum value. :( how to find the maximum?

View 4 Replies View Related

C++ :: Increasing Integers Automatically Within Two Dimensional Array

Nov 15, 2013

I am new to programming all together but i have been writing a program in c++ and im coming up against an issue with my array.

#include<iostream>
#include<iomanip>
#include<string>
using namespace
int main () {
int a ;
int b ;
char answer ('Y') ;

[Code] ....

I am trying to get the program to increase say year one by 1 when the condition is met i have tried

if ( a >= 70 && a <= 100 && b == 1)
{grade [0][0] = 0 + 1;}

and

for (grade[0][0] = 0 ; a >= 70 && a <= 100 && b == 1 ; grade [0][0]++)
{grade [0][0]= 0 + 1 ;}

Now all that i want is that the array will take the information from int a and int b and then add 1 to the appropriate part of the array . I have tried putting it in deferent places but its not working for ether. the program will run but it will not add to the array.

View 13 Replies View Related

C++ :: Two Dimensional Array - Multiply Mxn Matrix Of Integers By Nxr

Jan 8, 2015

Using two-dimensional arrays, write a program which multiplies an mxn matrix of integers by an nxr

matrix of integers.

INPUT FIRST (2x2) MATRIX:

Type in 2 values for row 1 separated by spaces: 3 4

Type in 2 values for row 2 separated by spaces: 5 7

INPUT SECOND (2x2) MATRIX:

Type in 2 values for row 1 separated by spaces: 1 1

Type in 2 values for row 2 separated by spaces: 2 2

3 4

5 7

TIMES

1 1

2 2

EQUALS

11 11

19 19

View 1 Replies View Related

C++ :: Reading Integers From File And Storing Into Array?

Oct 28, 2014

My main issue is that when I store just one value into a simple integer variable, it gives me a big negative number.

My goal is to store a file that reads like this into arrays:

2014 1 23
2012 2 15
2013 1 25
etc

Where I would have the first column in an array, the second in another and the third in third array since I am not allowed to use multidimensional.

View 5 Replies View Related

C++ :: Read Set Of Integers Then Find And Print Sum Of Even And Odd Integers

Apr 21, 2014

I'm a beginner at c++ and I need to write a program that reads a set of integers and then finds and prints the sum of the even and odd integers. The program cannot tell the user how many integers to enter. I need to have separate totals for the even and odd numbers. what would I need to use so that I can read whatever number of values the user inputs and get the sum of even and odd?

View 2 Replies View Related

C :: Program That Asks The User To Type 10 Integers Of Array?

Mar 2, 2014

Write a program that asks the user to type 10 integers of an array. The program will then display either "the array is growing", "the array is decreasing", "the array is constant", or "the array is growing and decreasing."

(Hint: compare consecutive numbers in array and check whether it is increasing or not or constant) I wrote the program very well, but do I have to use "bool" like in c++ ? I haven't known how to code in c++ yet.So, I don't want to use "bool" in C.

I also wrote code about it but it looks like backslash and one,two,three,four,five how can I do it like this ?

View 4 Replies View Related

C :: Get First Integer From A File And Assign It To A Variable And Others Integers To Array

Jun 2, 2013

what I need is to get the first integer from a file and assign it to a variable and the others integers to an array. Example: Thats my file content 5 4 6 7 8 0 and that would be the code:

Code:

struct Array{
int n;
int *v;
}

[code]....

View 8 Replies View Related

C :: Turning String Of Numbers Into Array Of Individual Integers

Mar 2, 2014

In my program, I am fed a string that contains integers such as Code: *str = "45678" and my program is simply supposed to read the number in and store each given number in a separate spot in an integer array. So basically, when my program has finished running it should be stored like:

Code:

arr[0] = 4
arr[1] = 5
arr[2] = 6
arr[3] = 7
arr[4] = 8

The way I have been attempting to do this was just copying it to the array like so:

Code:

arr = malloc(sizeof(int) *(strlen(str));
for (i=0; i<strlen(str); i++) {
a->digits[i] = str[i];
}

however, this just seems to return an impossibly high garbage value when I do. I'm assuming the way I'm trying to store it is 'illegal', but I cant seem to find online a proper way to do it.

View 2 Replies View Related







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