Click here to Skip to main content
15,892,575 members
Please Sign up or sign in to vote.
1.71/5 (3 votes)
See more:
ex: input is : 1 20 30 27 89

arr[0]=1
arr[1]=20
arr[2]=30



and so on how i can do this ?
Posted
Updated 22-Jun-17 20:40pm
Comments
Member 10651065 7-Mar-14 7:30am    
and one more thing the number of integer is less than or equal to 100
Vedat Ozan Oner 7-Mar-14 7:33am    
:) your homework will be completed today part by part.
Richard MacCutchan 7-Mar-14 9:24am    
Please do not repost. You already have a solution to this question.

 
Share this answer
 
#include<stdio.h>
#include<stdlib.h>

int main()
{

int i,j,n;
scanf("%d",&n); // scanning the size of the array
int *a = malloc(n*sizeof(int));
char temp;
for(i=0;i<n;i++) {

scanf("%d%c", &a[i],&temp); // scanning for both the integer and the space(as a character)

if(temp == '\n') // when temp == enter (\n),i.e, when the user presses enter
{

for(i=0;i<n;i++){ // for loop to print the array runs
printf("%d ",a[i]);

}
}
}

}
 
Share this answer
 
v2
You just have to use
yourString.Split(' ');
==> an array of string, then convert each of them into integers.

For conversion you should use:
int.TryParse()


If you are using C, you should read the next: http://www.cplusplus.com/forum/beginner/87238/[^]
 
Share this answer
 
v4
Comments
Vedat Ozan Oner 7-Mar-14 7:37am    
no. that is not right. we are using C.
Vedat Ozan Oner 7-Mar-14 7:44am    
still not right. there is no such thing in C, vectors, templates etc.
You should try something like,
C++
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main(void)
{
char b[] = "1.5, 2.5,3.5,4.5,5.5,6.5,7.5,,,10.5,11.5,12.5,,,15.5,16.5,17.5,18.5,19.5,20.5\n";
double c[21];
char *pptr = b;
b[strlen(b)-1]=',';
for (int i = 0; i < 21; i++) {
    char *ptr = strchr(pptr, ',');
    if (ptr) {
        *ptr = 0;
        c[i] = atof(pptr);
        pptr = ptr + 1;
        }
    }

for (int i = 0; i < 20; i++){
    fprintf(stdout, "%7.2lf", c[i]);
    }
return 0;
}

-KR
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900