no repeating values in array - c

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;
}

Related

how to put an option like this " try again? Y/N"?

I have been trying to put an option that would ask the user to try again or not, but I was unsuccessful in doing that. How do I do it?
#include <stdio.h>
int largest(int[], int);
int main()
{
char repeat;
int arr[5];
int i;
while (repeat == "y" || "Y")
{
for (i = 0; i < 5; i++)
{
printf("\nInput a number at index %d: ", i);
scanf("%d", &arr[i]);
}
for (i = 0; i < 5; i++)
printf("Element[%d] = %d\n", i, arr[i]);
printf("Largest in given array is %d\n", largest(arr, 5));
}
}
int largest(int arr[], int n)
{
int i;
int max = arr[0];
for (i = 1; i < n; i++)
if (arr[i] > max)
max = arr[i];
return max;
}
You need to initialize repeat to 'y' so it will go through the while loop at least once:
char repeat = 'y';
Your while loop needs to change to:
while (repeat == 'y' || repeat == 'Y') {
Then as the last thing in your while loop, after the printf, you'll want something like this:
printf("\nTry again? (Y/N) ");
scanf(" %c%*[^\n]",&repeat);

Can we scanf an array?

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]);

C program to insert elements into an array until user inputs a 0 or less number

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])

C Program triangle

I need to prompt a user to enter a number 'n' then print print stars in separate lines, for exanple if the user enters 5 the following should be printed (Using a while loop)
*
**
***
****
*****
****
***
**
*
The code i have doesn't output that
int n, x = 1, y = 1;
printf("enter a number : ");
scanf_s("%d", &n);
while (x <= n){
x++;
while (y <= x){
while (y >= n){
y--;
printf("*");
}
printf("\n");
}
system("pause");
}
}
This should work for you:
#include <stdio.h>
int main() {
int number, count, characterCount;
printf("Please enter a number:\n>");
scanf("%d", &number);
for(count = 1; count <= number; count++) {
for(characterCount = 1; characterCount <= count; characterCount++)
printf("*");
printf("\n");
}
for(count = 1; count < number; count++) {
for(characterCount = number-1; characterCount >= count; characterCount--)
printf("*");
printf("\n");
}
return 0;
}
Edit after comment:
Solution with while loop:
#include <stdio.h>
int main() {
int number, count = 1, characterCount;
printf("Please enter a number:\n>");
scanf("%d", &number);
while(count <= number) {
characterCount = 1;
while(characterCount <= count) {
printf("*");
characterCount++;
}
printf("\n");
count++;
}
count = 1;
while(count < number) {
characterCount = number-1;
while( characterCount >= count) {
printf("*");
characterCount--;
}
printf("\n");
count++;
}
return 0;
}
You can make it using only 2 for loops like this:
int i,k,n,flag=0;
printf("Enter Number of Rows :");
scanf("%d",&n);
for(i=-n;i<=n;i++)
{
if(i < 0)
{
i = -i;
flag=1;
}
for(k=(n-i)+1;k>0;k--)
{
printf("*");
}
if(flag==1)
{
i=-i;flag=0;
}
printf("\n");
}
For example if user enter value 2 it will print
*
**
***
**
*

putting a number in a hollow shape

I want to put number that can be one,two,three,four and so forth digits in the shape. Nonetheless, extra spaces is shifted. Besides, center is shifted. I trying different ways,however,i can't. How can i fix the problem. Thank you for all appreciated answer.
output example is:
http://i.stack.imgur.com/Kozr1.png
#include <stdio.h>
/* Function Prototypes */
int countNumber(int number);
int main() {
int n; /* number to put in the center of the shape */
int column, row; /* take values of column and row via user */
int columnCount, rowCount; /* count number of column and row */
int i; /* count how many spaces remained */
printf("Enter column:\n>");
scanf("%d",&column);
printf("\nEnter row:\n>");
scanf("%d",&row);
printf("\nEnter number for center:\n>");
scanf("%d",&n);
if((10<column && column<40) && (10<row && row<40)){
for (columnCount = 0; columnCount < column; columnCount++) {
for (rowCount = 0; rowCount < row; rowCount++) {
/* Middle Row */
if (columnCount == column/2) {
printf("#");
for (i = 0; i < ((row*2-3)-countNumber(n))/2;i++)
printf(" ");
printf("%d", n);
for (i = 0; i < ((row*2-3)-countNumber(n))/2;i++)
printf(" ");
printf("#");
break;
}
if (columnCount==0 || columnCount==column-1 || rowCount==0 || rowCount==row-1)
printf("# ");
else
printf(" ");
}
printf("\n");
}
}
else
printf("Please enter value btw 10-40");
return 0;
}
/* Function */
int countNumber(int number) {
int count;
for(count=0;0<number;count++) /* the number how many decimal places have */
number/=10;
return count;
}
this one works with any number
#include <stdio.h>
int digits(int n)
{
int i;
for(i=0;n;i++,n=n/10);
return i;
}
int main()
{
int i,j;
int column,row;
int n,cnt;
printf("Enter column: ");
scanf("%d",&column);
printf("Enter row: ");
scanf("%d",&row);
scanf("%d",&n);
cnt=digits(n);
for(i=0;i<row;i++)
{
for(j=0;j<column;j++)
{
if(i==0 || i==(row-1) || j==0 || j==(column-1))
printf("*");
else if((i==(row/2)) && (j==((column-cnt)/2)))
{
printf("%d",n);
j=j+(cnt-1);
}
else
printf(" ");
}
printf("\n");
}
return 0;
}
This should work!
It still has a small margin of error but I try to solve it
/*Import*/
#include <stdio.h>
/*Function Prototyp*/
int countNumber(int number);
int main() {
/*Variable*/
int number, column, row;
int columnCount, rowCount;
int i;
printf("Enter column:\n>");
scanf("%d",&column);
printf("\nEnter row:\n>");
scanf("%d",&row);
printf("\nEnter number for center:\n>");
scanf("%d",&number);
for (columnCount = 0; columnCount < column; columnCount++) {
for (rowCount = 0; rowCount < row; rowCount++) {
//Middle Row
if (columnCount == column/2) {
printf("*");
for (i = 0; i < ((row*2-3)-countNumber(number))/2;i++)
printf(" ");
printf("%d", number);
for (i = 0; i < ((row*2-3)-countNumber(number))/2;i++)
printf(" ");
if (countNumber(number % 2 == 0))
printf(" *");
else
printf("*");
break;
}
if (columnCount==0 || columnCount==column-1 || rowCount==0 || rowCount==row-1)
printf("* ");
else
printf(" ");
}
printf("\n");
}
return 0;
}
int countNumber(int number) {
int count;
for (count = 0; number > 0; count++)
number = number / 10;
return count;
}

Resources