C++ :: Turning Digits Of Integer Into Array Elements?
Nov 19, 2013
I have an integer that the user enters. I need each digit of the integer to be set as an element of an array. the integer could also be entered as an array, but I need the user not to have to enter each element and press ENTER.
View 5 Replies
ADVERTISEMENT
Oct 11, 2014
My problem needs to prompt the user to input an integer and then outputs both the individual digits of the number and the sum of the digits. An example would be entering 8030 and it spits out 8 0 3 0 as well as 8+0+3+0=11 and it needs to work with negative numbers.
Code:
#include <iostream>
#include <iomanip>
using namespace std;
int main() {
int base;
[Code] ....
Now I don't know if any of this is right a hint my professor gave us is that to get the fourth digit you would do base mod 10 and to get the first digit you do base divided 1000...
Code:
{
int power;
int counter=0;
int value=1;
cout << "Enter the power of 10 you want: ";
[Code] ....
View 2 Replies
View Related
Jan 10, 2015
Write a program using user-defined function which accepts an integer array and its size as arguments and assign the elements into a two dimensional array of integers in the following format: If the array is 1,2,3,4,5,6, the resultant 2D array is
1 2 3 4 5 6
1 2 3 4 5 0
1 2 3 4 0 0
1 2 3 0 0 0
1 2 0 0 0 0
1 0 0 0 0 0
View 1 Replies
View Related
Feb 4, 2014
I'm just learning and C. Here is a code snippit from a program that will compile. It's function is to validate credit card numbers. I have an error I can't find though. the last print statement shows the conversion in reverse string (as integers). Here is the code:
int main (void)
{
char cn[17];
char *cardtype;
int n0,n1,n2,n3,n4,n5,n6,n7,n8,n9,n10,n11,n12,n13,n14, n15;
int s1,s2,s3,s4,s5,s6,s7,s8;
int oddsum;
int sum;
int total;
int validate;
}
[code]....
View 14 Replies
View Related
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
May 5, 2013
Just wanted to share a program I made. It was the answer to one of the end chapter exercises in the C programming book I'm using, asking the reader to create a program that adds all the digits of an integer.
Code:
/* Program to calculate the sum of the digits in an integer */
#include <stdio.h>
int main () {
int number, right_digit, sum = 0;
[code]....
View 7 Replies
View Related
Apr 23, 2013
I want to convert the integer into a string.
int x = 693;
char chararr[max];
In my homework, x is unknown. but don't worry, I wont ask for the full code. I just need the part where you change the int into a string/array of char.
I'm thinking in circles from where to start?
View 2 Replies
View Related
Jan 31, 2013
I want to write a function which take an integer and return the number of digits in it i.e
int i = 123456
func(i) {
some code
}
output
the number of the digits are 6
View 9 Replies
View Related
Apr 18, 2013
Write a full C++ program that inputs three-digit integer, separates the integer into its individual digits and prints the digits separated from one another. For example, if the user types 549, the program should print;
5 4 9
View 5 Replies
View Related
Feb 18, 2015
Code:
int exploder(int number,int array[]) {
int functi = 0;
int digit = number % 10;
while (number > 0) {
[Code] ....
View 2 Replies
View Related
Feb 7, 2012
I have a char *pch that points to an integer digit 1 or 2 or ... 9. To get the character that's 1 less, instead of converting to int, minus 1, then converting back to char, I tried (*pch -1) and that seemed to work. I suppose that's because the particular character encoding on my system is such that the digits are encoded in the same order and spacing as the integers they represent. So the question is does this "convenience" feature hold true for all character encoding systems?
View 10 Replies
View Related
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
Oct 25, 2013
I am trying to assign the integer value to unsigned char array. But it is not storing the integer values. It prints the ascii values. Here the code snippet
Code: uchar uc[100];
for(i=0;i<100;i++)
{
uc[i] = i;
}
The values which are stored in uc[] is ascii values.I need the integer values to be stored in uc[]. I tried to do it with sprintf. but the output is not as expected. if I print the uc[i] it should diplay the value as 0,1,2....99.
View 9 Replies
View Related
Oct 9, 2013
The aim is : If i have a natural number [max 5 digits] (say) n=13579. Then a[4]=1, a[3]=3, a[2]=5. a[1]=7. a[0]=9.
I start with a[4]= n/10000 .. then n= n - a[4]*10000;
then a[3]= n/1000. .. n= n - a[3]*1000;
i.e a[i]= n/10^i .... n= n - a[i]*10^i;
To reduce code size i used a for loop. The code is free from any syntax errors.
Its the "output" that is weird. I enter 13579 and what the program process is:
a[4]=1 a[3]=3 a[2]=5 a[1]=8 a[0]=5
Same goes for other numbers too.. leaving the most significant digit , the rest digits are just out of the logic!!
I am using CodeBlocks 12.11 and compiler is GNU GCC compiler.
Here is the output window: [URL] ....
Here is the code:
#include<iostream>
#include<math.h>
using namespace std;
int main() {
int n,a[5],i,t,dec,divisor; // n: storing number; t: copy of n,
[Code] .....
View 2 Replies
View Related
Dec 10, 2014
I have a problem with transforming a string, for example
"13 + 19"
and store this in a list as seperate integers,
list = {13, 19};
and another list with the +, -, /:
list2 = {+};
this is my function:
int evaluate(char* formula, int* result) {
struct List *listofintegers = list_create(); //creates a list, this is the structure:
/*
struct ListNode {
int value;
struct ListNode* next;
[Code] ....
This is how i execute my function in my main.c:
int value;
evaluate("19 + 16", value);
This is what i get in my prompt:
I will also have to seperate the +, - and / in another list,
I don't even know how i can get my string when char* formula is given as an argument...
View 2 Replies
View Related
May 14, 2013
I don't know how to start with it, first thing that came up to my mind was "loop through an array of bits" in bitmap (monochromatic), but I can't get the bits. Here's code I have:
#include <iostream>
#include <fstream>
using namespace std;
unsigned char* readBMP(char* filename) {
[Code] .....
No errors, it's working fine but I just can't get the bits of picture. I've tried to "cout" values form header (width, height) and they're ok, but I wonder how to get representation of picture itself. I thought that this code will give me something like array of 1 and 0.
I have to convert black and white (monochromatic) bmp image to min. finite automata.
View 12 Replies
View Related
Jul 21, 2013
So I have a code like this one below :
void get_sum( INNER_ID id, vector<INNER_ID>& dont_check ) {
vector<INNER_ID> below = get_below( id );
vector<INNER_ID>::iterator second;
[Code]....
I use this algorithm for my "crappy" physic engine, so the point of this algorithm is to get the sum of mass below an object. get_below( id ) function can get the ids of what object is below them.
But before I need ids of the object below them to apply impulse, force, and some other physic stuff.
One object doesn't neccesarrly rest on top of one object, it can rest on 2 object or more.
when I look at it, it resemble a tree, maybe it's not. I just don't really know very much about tree algorithm
I cannot optimize a recursive code so I think, I better turn this into an iterative but I cannot seem to find a way to do that
View 4 Replies
View Related
May 28, 2014
Im programming a roguelike game using visual c++ Microsoft express 2010 and i made a multidimensional array for my first map. I have the walls as # and was wondering how i could turn those into ascii symbol 219. Also i need to know how to turn specific text certain colors.
View 5 Replies
View Related
Jan 21, 2013
Write a function that takes an array and returns true if all the elements in the array are positive, otherwise, it returns false.
View 6 Replies
View Related
Aug 13, 2014
I have just started learning C language, I have read that array name is the pointer to the first element of array.
So,technically both a and a[0] should have different memory address and a should hold address of a[0],
But when I declare an array and print the address:
int a[5]={2,4,6,8,10};
printf("The address of a and a[0] are &d and %d",&a,&a[0]);
Output is: The address of a and a[0] are 2358832 and 2358832
Why I am getting the same result, shouldn't I be getting different memory address for both a and a[0].
View 2 Replies
View Related
Apr 5, 2013
I've a file that looks like this:
84484-37.96-Castor, Kathy
39050-69.68-Chandler, Ben
26183-70.84-Costello, Jerry
I have successfully read each element the id, grade and name into 3 separate array. Now i need to add a new student with an id and grade
How do i do this?
This is what I have.
int addStudent( int Iarray[], double dArray[], string sArray[], int newID,
double newGrade, string newName, int size ) {
char ready;
int index;
cout << endl;
cout << "Enter new student ID number : ";
[Code]...
View 4 Replies
View Related
Oct 27, 2014
I have a question regarding the elements of an array. Suppose I have a 3 by 3 string array (char x[3][4] ) , and I initialize all the elements to x's , the array would then look like this :
xxx
xxx
xxx
I'm curious if there will be a value if I try to access and element outside the array. As I have to write a code to determine if I have reached the end of an array. The only way I can think of is to border the entire array with o's , making it look like this :
ooooo
oxxxo
oxxxo
oxxxo
ooooo
Is there any other way to do this?
View 5 Replies
View Related
Oct 19, 2013
Trying to concatenate the 1st and 2nd element from an array of chars. Then this to be used as a hex value.
so for example
specs[0]='f'
specs[1]='4'
concatenate_value=0xf4
View 9 Replies
View Related
Apr 15, 2014
Write the definition of a function reverse , whose first parameter is an array of integers and whose second parameter is the number of elements in thearray . The function reverses the elements of the array . The function does not return a value .
Code:
void reverse(int a[], int num) {
for ( int i=0; i <= num/2 ; i++){
int temp = a[i];
a[i] = a[num-i-1];
a[num-i-1] = temp;
} }
This is supposed to be the answer but I'm not quite sure why this is. I understand everything up until the actual loop. For one, shouldn't "int i" be declared outside the loop (I thought perhaps this was an error in the solutions)?
The main thing that I do not understand is the conditional statement.
Code: i<=num/2;
I don't understand why the "num/2" is necessary here. Also I can't really remember but is there a command that actually reverses an array?
View 5 Replies
View Related
Sep 18, 2013
I'm trying to set all the elements of my array to 0 but nothing seems to be working.
.h
#ifndef ServerGroup_h
#define ServerGroup_h
class ServerGroup {
public:
ServerGroup(const int&);
[Code] .....
I want to set each element of the array in servers to 0 based on what is passed into size by numArray.
View 9 Replies
View Related
Apr 11, 2014
I am trying to write a program that reverses the elements of an array by using an function 'myreverse'. The function should reverse original array and return nothing.
In my program, the function 'myreverse' is not getting invoked and the original array is being displayed as it is.
#include <iostream>
using namespace std;
void myreverse(int arr[],int n) {
int *p=&arr[n-1];
int temp;
for(int i=0;i<n;++i)
[Code] .....
View 5 Replies
View Related