The problem is to input some n elements in a line and make an array in c language.
The format of input is This.
input:
15 //number of elements
1 4 4 2 3 5 6 x x x x x x 5 7 // elements
I tried it by using a scanf function, but it didn't work.
char* tree;
int n;
scanf("%d", &n);
tree = (char*)malloc(sizeof(char) * n);
for (int i = 0; i < n; i++)
{
scanf("%c", &tree[i]);
}
what's the problem of this code?
Some what better program would be
#include <stdio.h>
#include <stdlib.h>
int main()
{
int n = 0;
int* numbers = NULL;
printf("How many elements do you want to store:");
do {
if(1 != scanf("%d",&n)) {
puts("error in scanf");
n = 0;
}
if(n <= 0)
printf("Enter a positive number:");
}while(n <= 0);
numbers = malloc(n * sizeof (int));
if( numbers == NULL) {
puts("malloc failed");
return 0;
}
printf("\n Enter %d number of elements:", n);
for(int i = 0; i<n;i++)
if (1 != scanf("%d", &numbers[i]))
printf("error reading input\n");
printf("\n The Entered elements are: ");
for(int i = 0; i < n; i++)
printf("%d ", numbers[i]);
free(numbers);
numbers = NULL;
return 0;
}
Related
I want to input an array of integers then print out the even numbers from the inputted numbers..
example is if I input 2466688992,
it will output 24666882;
I have a my code below:
#include<stdio.h>
int main()
{
int a[5],i;
printf("Enter array of numbers: ");
scanf("%d",&a);
for(i=0; i<sizeof(a); i++){
if(a[i]%2==0)
printf("%d",a[i]);
}
getch();
return 0;
}
It resulted into garbage : 2468000075416640419940000004225568000
This is the function that prints even numbers in an integer :
#include<stdio.h>
int main(){
int num,rem,even=0,digit;
printf(" Enter an integer number: ");
scanf("%d",&num);
printf("\n The even digits present in %d are \n",num);
while(num>0){
digit = num % 10;
num = num / 10;
rem = digit % 2;
if(rem == 0)
even++;
printf("\n %d.",digit);
}
return 0;
}
You should scan the array as a string (unless you want to impose the number of items in the array), and then parse the string to store the different numbers:
long a[50];
char buf[1024];
printf("Enter array of numbers: ");
scanf("%s",buf);
int len = strlen(buf);
int j = 0;
for (int i = 0; i < len; ) {
long sign = 1;
long n = 0;
if (buf[i] == '+') {
++i;
}
else if (buf[i] == '-') {
sign = -1;
++i;
}
if (isdigit(buf[i])) {
while (isdigit(buf[i])) {
n = 10 * n + buf[i++] - '0';
}
a[j] = n * sign;
}
else
i++;
}
for (int i = 0; i < j; i++)
if (!(a[i] ℅ 2)) // true if even
printf("%ld ", a[i]);
This will store all your digits in your array a of size j.
Edit: if you are talking about digits then its easier:
char buf[1024];
printf("Enter array of numbers: ");
scanf("%s",buf);
int len = strlen(buf);
for (int i = 0; i < len; i++)
if (isdigit(buf[i]) && !((buf[i] - '0') ℅ 2)) // true if even, note that '0' equals 0x30 so there is no need to sub it to check for odd/even in reality.
printf("%c ", buf[i]);
I'm trying to make a C program to insert elements into an array until user inputs a 0 or less number, as the title says. But when I print the array out, it doesn't show the numbers I inputted. I have tried using a while as well as do-while loops but without success.
#include <stdio.h>
int main() {
int data[100];
int i;
for (i = 0; i < 100; i++) {
printf("Input your number:\n");
scanf("%d", &data[i]);
if (data[i] <= 0) {
break;
}
}
printf("Your array:");
int n = sizeof(data[i]);
for (int i = 0; i < n; i++) {
printf("%d ", &data[i]);
}
}
Try this:
#include <stdio.h>
int main() {
int data[100];
int i;
int counter = 0;
for (i = 0; i < 100; i++) {
printf("Input your number:\n");
scanf("%d", &data[i]);
counter++;
if (data[i] <= 0) {
break;
}
}
printf("Your array:");
for (int j = 0; j < counter - 1; j++) {
printf("%d ", data[j]);
}
}
The problem was that you had printf("%d ", &data[i]); instead of printf("%d ", data[i]);.
And also you've trying to get the sizeof() of an element data[i], not the size of the whole array. That's why there's counter in my code.
int n = sizeof(data[i]);
this is wrong, you want
int n = i;
sizeof(data[i]) gives you the size of an int (4 on my machine)
On the other hand, you need to check the result of scanf, if a bad input is entered do not increment the counter, something like:
int i = 0;
while (i < 100)
{
int res = scanf("%d", &data[i]);
if (res == EOF)
{
break;
}
if (res == 1)
{
if (data[i] <= 0)
{
break;
}
i++;
}
else
{
// Sanitize stdin
int c;
while ((c = getchar()) != '\n');
}
}
Finally, scanf wants a pointer to the object, but this is not the case of printf:
printf("%d ", &data[i])
should be
printf("%d ", data[i])
Here are the instructions I was given:
If the command word is find, read an additional integer and search the data set for that integer.
If the command word is print, print the array
Any other command word is an error.
No command word will be longer than 20 characters.
After reading the n+1 values, there will be one more integer (k) read from the keyboard.
Search the array for the value k. If found, print the location where k was found. (1 = data value, n = last data value).
If k is not found, print not found. This is not an error.
If there are more than one value k in the data, only print the location of the first one.
#include <stdio.h>
int main (void) {
int n;
scanf ("%d", &n);
if (n < 1) {
printf ("Error: one or more values must be provided.\n");
return 1;
}
int x [n];
int a;
a = 0;
while (a < n) {
scanf ("%d", x [a]);
a = a + 1;
}
int k;
scanf ("%d", &k);
int i;
i = 0;
while (i <= n-1) {
if (x[i] == k) {
break;
}
i = i + 1;
}
if (i < n) {
printf ("%d\n", k+1);
} else {
printf ("not found\n");
}
printf ("Error: invalid command\n");
return 0;
}
Suggested Strategy:
After reading the array data, read a string.
If the string is find, read integer k and perform a search.
If the string is print, do not read k, just print the data in the array.
If the string is not find or print, handle the error.
Shai'Tavia, I hope my answer will help you see how you may make your code work. You've got the first part down, but you will need to compare the command string given by the user to then make a decision on what to do next.
#include <stdio.h>
#include <string.h>
#define ARRAYLENGTH 8
void printArray(int *array, int length)
{
for (int i = 0; i < length; i++)
printf("%d ", array[i]);
printf("\n");
}
void search(int *array, int key)
{
int flag = 0;
for (int i = 0; i < ARRAYLENGTH; i++)
{
if (array[i] == key && flag == 0)
{
printf("found %d at index: %d\n", key, i);
flag = 1;
}
}
if (flag == 0)
printf("not found\n");
}
int main(void)
{
char command[20];
int indx = 0;
int array[] = {1, 4, 6, 8, 43, 61, 34, 2};
int n, flag = 0;
printf("How many times will we run?");
scanf("%d", &n);
if (n < 1)
{
printf("Error: one or more values must be provided.\n");
return 1;
}
do
{
printf("Enter the command word:");
scanf("%s", command);
if (strcmp(command, "find") == 0)
{
scanf("%d", &n);
search(array, n);
}
else if (strcmp(command, "print") == 0)
printArray(array, ARRAYLENGTH);
else
printf("Command not found\n");
} while (--n > 0);
printf("What is your final interger?");
scanf("%d", &n);
search(array, n);
return 0;
}
I need to make a program that stores numbers inside of an array. But it must have no duplicate elements.
int x;
int z[8];
for( x = 0; x<8;x++)
printf("number: ");
scanf("%d",&z[x]);
}
for( x=0;x<8;x++) {
printf("%d ",z[x]);
}
First, initialize the array, so that you do not end up reading an uninitialized value and fail the test.
int user_nums[6] = {0};
Next, you need to have another check in the for loop, to read the number again if it is a duplicate.
The code will look like this.
#include<stdio.h>
int main(){
int x,y;
int exists = 0;
int user_nums[6] = {0};
for( x = 0; x<6;x++){//for loop to get the players selected numbers
do {
exists = 0;
printf("Enter a number(from the #'s 1-42): ");
scanf("%d",&user_nums[x]);
for(y =0; y < x; y++) { //to check for duplicates
if (user_nums[x] == user_nums[y])
{
printf("Number already exists\n ");
exists = 1;
break;
}
}
}while (user_nums[x]<1 || user_nums[x]>42 || exists);//accepts only numbers from 1-42 which are not duplicates (continous to ask you for a number until condition is met).
}
printf("Your numbers: \n");
for( x=0;x<6;x++){
printf("%d ",user_nums[x]); // prints the numbers you inputed.
}
return 0;
}
The following code could work in O(n):
#include<stdio.h>
int main()
{
int user_nums[6];
int index[50];
for (int i = 0; i != sizeof(index) / sizeof(index[0]); ++i)
index[i] = -1;
for (int i = 0; i < sizeof(user_nums) / sizeof(user_nums[0]); ++i) {
for (;;) {
printf("Enter a number(from the #'s 1-42): ");
scanf("%d", user_nums + i);
if (user_nums[i] < 1 || user_nums[i] > 42) {
printf("wrong number\n");
continue;
}
if (index[user_nums[i]] != -1) {
printf("dump number\n");
continue;
}
index[user_nums[i]] = i;
break;
}
}
printf("Your numbers: \n");
for(int i = 0; i < 6; ++i)
printf("%d ", user_nums[i]);
return 0;
}
Forgive me, I'm a C programming rookie. What I need to do is take values from standard input and store them in an array which is to be sorted later on down the line.
The method of entry for the user is one number on one line at a time (i.e. enter a number, press enter, enter number, press enter, etc..). When the user is done entering numbers, they press ENTER without providing a number.
My code for accepting the values and storing them is as follows. You'll probably see the issue immediately, but I'm not seeing it.
#include <stdio.h>
#define MAX 100
int main()
{
int n, i, array[MAX];
printf("Enter a list of integers\n");
for(i = 0; i <= MAX; ++i){
printf("> ");
if (scanf("%d", &n) == -1)
break;
else
scanf("%d", &n);
array[i] = n;
}
printf("The array is %d", *array);
return 0;
}
The picture below is how the program should run. I have the sort code already, and it seems to work quite well. Your help is greatly appreciated.
You have it doing what you want, you just need a few tweaks. First, enter doesn't return -1, to keep it simple you need to enter ctrl+d to stop input. After your final input, just hit ctrl+d. Take a look:
#include <stdio.h>
#define MAX 100
int main()
{
int n, i, array[MAX];
printf("Enter a list of integers [ctrl+d] to end\n");
for(i = 0; i <= MAX; ++i){
printf("> ");
if (scanf("%d", &n) == -1)
break;
array[i] = n;
}
puts ("");
int z;
for (z = 0; z < i; z++)
printf("The array is %d\n", array[z]);
return 0;
}
output:
Enter a list of integers [ctrl+d] to end
> 1
> 2
> 3
> 4
> 5
>
The array is 1
The array is 2
The array is 3
The array is 4
The array is 5
Here is the updated previous answer to exit on enter.
#include <stdio.h>
#define MAX 100
int main()
{
int n, i, array[MAX];
char num[MAX];
int res;
printf("Enter a list of integers [ctrl+d] to end\n");
for(i = 0; i <= MAX; ++i){
printf("> ");
fgets(num, sizeof(num), stdin);
res = sscanf(num, "%d", &n);
if(res != 1)
break;
n = atoi(num);
array[i] = n;
}
puts ("");
int z;
for (z = 0; z < i; z++)
printf("The array is %d\n", array[z]);
return 0;
}