It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 11 years ago.
How to take some unknown number of integers from input and display them on the console?
The number of values will be given through console.
Well the prototype for main is:
main(void) or
main(int argc, char *argv[]);
This are the command line arguments of the program
you can iterate through them with something along this lines
int i = 0;
for (i=0; i < argc; i++) {
printf("%s" argv[i]);
}
Untested but should be enough to get you going.
You can dynamically allocate memory for the numbers using malloc.
int count=0;
scanf("%d",&count);
int* numbers=malloc(sizeof(int)*count);
//take integers from input here
free(numbers);
normally your main method should look something like this
int main(int argc, char** argv)
when you run the you program you will supply a number of arguments to it, say n numbers, so you will have n+1 arguments in total (recall that your program name is argument 0!). therefore you can do something like:
int* array = malloc(sizeof(int) * (argc-1));
int i;
for (i = 0; i < argc-1; i++) {
array[i] = atoi(argv[i+1]);
}
then you can do:
for (i = 0; i < argc-1; i++) {
printf("%d ", array[i]);
}
sorry if there are any syntax errors
hope this helps
Print one-by-one as you go (ignore the first: the number of numbers)
int main(void) {
<READ_NUMBER>; // and promptly ignore it
while (<READ_NUMBER>) {
printf(<PRINT_NUMBER_JUST_READ>);
}
return 0;
}
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 10 months ago.
Improve this question
For a school assignment, I am to Create variables in main() that will store the counts and pass pointers to these variables to your functions so that the functions can modify the variables via the pointers. This is a school assignment so rather than someone Give me the answer, I would prefer is someone could help point me in the right direction of using pointers. The Code does work, but not in the way I would like yet.
the code is as follows
void myFunction(int *letters, int *numbers, int *otherCharacters){
}
int main(int argc, char * argv[]) {
// Code for command line argument
if (argc == 2) {
int letters = 0;
int numbers = 0;
int otherCharacters = 0;
int totalCharacters;
int length = strlen(argv[1]);
for (int i = 0; i < length; ++i){
if (isalpha(argv[1][i]) != 0)
++letters;
if (isdigit(argv[1][i]) != 0)
++numbers;
if (isdigit(argv[1][i]) == 0 && isalpha(argv[1][i]) == 0)
++otherCharacters;
}
totalCharacters = letters + numbers + otherCharacters;
printf("%i letters\n%i digits \n%i other characters\n%i characters total\n", letters, numbers, otherCharacters, totalCharacters);
}
I am hoping to rather than change the values of letters, numbers, otherCharacters, and totalCharacters in the main function use pointers to do so in myFunction(). any help on how to use pointers to do so would be much appreciated. Again, I am not asking for an answer, as I would like to complete this assignment myself.
Seems like the function is supposed to look at a string and tell you how many letters, numbers, and other characters there are. It needs to take the counts as pointers, and the string.
void countCharacters(const char *string, int *letters, int *numbers, int *other) {
....
}
Because they are pointers, when incrementing them you need to dereference them first to get their values. Instead of letters++ it would be (*letters)++.
And we can replace the main code to show how you'd call this.
int main(int argc, char * argv[]) {
// Exit early to avoid deeply nesting all the code.
if (argc != 2) {
perror("please supply a string");
return 1;
}
int letters = 0;
int numbers = 0;
int other = 0;
// Pass in the string (already a pointer) and the counts as pointers.
countCharacters(argv[1], &letters, &numbers, &other);
int total = letters + numbers + other;
printf("%i letters\n%i digits \n%i other characters\n%i characters total\n", letters, numbers, other, total);
}
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
I have some code and, I want to use Neon instruction to change it, but I really don't know how to complete it... Can anyone help me?
void add(int n,float *a,float *b,float t) {
int i, size = (n+2) * (n+2);
for(i = 0; i < size; i++)
a[i] += t * b[i];
}
By using NEON intrinsics.. something like this:
void add(int n,float *a,float *b,float t) {
int i, size = (n+2) * (n+2);
float32x4_t temptt = vdupq_n_f32(t);
for(i = 0; i < size; i+=4) {
float32x4_t temp1 = vld1q_f32(a+i);
float32x4_t temp2 = vld1q_f32(b+i);
temp1 = vmlaq_f32(temp2, tempt, temp1);
vst1q_f32(a + i, temp1);
}
}
This does 4 iterations at once, and would only work when size is a multiple of 4.
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I'd like to know how many element I have between two pointers to a tab, with a function.
For example in this code, nb would be 20.
int main(void)
{
int t[50] = {0};
int nb;
nb = finding_number_element(&t[10], &t[30]);
return (0);
}
Have you got a idea?
Try
int finding_number_element(int *a, int *b)
{
return (b - a);
}
if a and b will be pointers to the same array this should work.
If you need something that doesn't care about the data type of t
#define ele_size(x) sizeof(x[0])
int main(void)
{
int t[50] = {0};
int nb;
nb = finding_number_element(ele_size(t),&t[10], &t[30]);
return (0);
}
int finding_number_element(unsigned int ui_ele_size, void *a, void *b)
{
unsigned int ui_pointer1 = (unsigned int)a;
unsigned int ui_pointer2 = (unsigned int)b;
return ((b - a)/ui_ele_size);
}
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 11 years ago.
I have been asked in an interview to swap the last and first digit in an integer using bitwise operators. Tried a lot but I could not find the solution. How can I do this?
Use int digits = log10(x) to get the number of digits.
Use int first = x / pow(10,digits) to get the first digit.
Use int last = x % 10 to get the last digit.
Put it all together and you have
int swapped = x + (last - first) * pow(10,digits) + (first - last)
A trivial solution:
def swap_digit(n):
x = str(n)
if len(x) < 2:
return x
return int(x[-1] + x[1:-1] + x[0])
EDIT: Added a quick and dirty C solution
#include <stdio.h>
#include <string.h>
int main()
{
int n = 123456789;
char buf[100];
int r = snprintf(buf, sizeof(buf), "%d", n);
char t = buf[0];
buf[0] = buf[r-1];
buf[r-1] = t;
int swap;
sscanf(buf, "%d", &swap);
printf("n = %d, swap = %d\n", n, swap);
return 0;
}
def swap(i):
s = list(str(i))
s[0], s[-1] = s[-1], s[0]
i = int(''.join(s))
return i
print swap(123456789) # 923456781
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 11 years ago.
I'm working in a small program using Language C.
I want to create a function which transform a decimal number to binary.
So i Have that:
#include< stdio.h>
int *binaire(int nb,int base)
{
int d,i=0,j;
int *tab= (int*) malloc(sizeof(int));
while(nb>0){
tab[i]=(nb%base);//inverser les bits
nb=nb/base;
i++;
}
return tab;
}
please Help
Thxs
sizeof returns the size in bytes, not in bits. May be you are looking for this:
int *tab= (int*) malloc(sizeof(int) * 8);
EDIT: As pointed in the comments, there are other problems here. Please check them too.
EDIT2: The previous code contains a bug in calculation. I am assuming that you are trying to store the bits in an int array. So the array will contain sizeof(int) * 8 integers, all either zero or one. So the code will be:
int arraySize = sizeof(int) * 8;
int *tab = (int *) malloc(arraySize * sizeof(int));
In fact you don't need to use full int to store a single bit. One byte character is enough to store a bit.
Somethign along those lines:
#include <math.h>
#include <string.h>
#include <stdio.h>
typedef struct value_
{
int* digits;
int nb_digits;
} value;
value* base_convert(int nb,int base) ;
value* make_value(int nb, int base)
{
value* v = malloc(sizeof(value));
if(v)
{
v->nb_digits = 1+round(log(nb)/log(base));
v->digits = malloc(sizeof(int)*v->nb_digits);
if(v->digits) base_convert(v, nb, base);
}
return v;
}
void destroy_value(value* v)
{
if(v->digits) free(v->digits);
free(v);
}
void base_convert(value* v, int nb,int base)
{
int i;
for(i=0;i<v->nb_digits;++i)
{
v->digits[v->nb_digits-i-1]=(nb%base);//inverser les bits
nb=nb/base;
}
return v;
}
int main()
{
int i;
value* v = make_value(137,2);
for(i=0;i<v->nb_digits;++i) printf("%d",v->digits[i]);
destroy_value(v);
return 0;
}
This needs :
a get_digit function to mask the rough access to value::digits
Number of digits is computed using log to be generic. The abse 2 version can be computed in a faster way using bitshifts.