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

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

Related

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

no repeating values in array

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

Loop is ignoring to increment the size of the array

In the main method below I'm trying to call a sort function and after function selects that latter from the user input it has to print the sort accordingly with the for loop at the end. But I have a warning that reads "loop will run at most once (loop increment never executed)" pointing at the array[arraySize]. Does it have to do with the return type or the other for loop above? What's happening here? Can anyone point out and explain please. Thanks much! Here's the code below:
int main()
{
long array[100], arraySize;
char sort;
long maxi = 100;
for(arraySize = 0; arraySize < maxi; arraySize++)
{
printf("Enter any positive integer, enter 0 to stop: ");
scanf("%li", &num);
if(num < 0) {
arraySize--;
printf("I said positive!");
count++;
}
else if(num == 0) {
maxi = arraySize;
}
else {
array[arraySize]=num;
arraySize--;
}
}
printf("Please enter A for ascending or D for descending order\n");
scanf("%s", &sort);
bubble_sort(array, arraySize, sort); //calling the sort function
printf(" Sorted list in the selected order:\n");
for (arraySize = 0; arraySize < num; arraySize++) {
printf("%ld \n", array[arraySize]);
puts("");
return 0;
}
}
EDIT: Thanks everyone for all your suggestions. I did make a few changes and here's what I have so far. Now it's skipping the A/D user input along with the bubble_sort function logic. Here's what it does as a final output: Note: long num is declared as a global variable!
int main()
{
long array[100], arraySize;
char sort;
long maxim = 100;
for(arraySize = 0; arraySize < maxim; arraySize++)
{
printf("Enter any positive integer, enter 0 to stop: ");
scanf("%li", &num);
if(num < 0)
{
arraySize--;
printf("I said positive! \n");
count++;
}
else if(num == 0)
{
maxim = arraySize;
}
else
{
array[arraySize]=num; //arraySize--;
}
}
printf("Please enter A for ascending or D for descending order: \n");
scanf("%c", &sort);
bubble_sort(array, maxim, sort); //calling the sort function
printf("Sorted list in the selected order:\n");
for (arraySize = 0; arraySize < maxim; arraySize++)
{
printf("%ld \n", array[arraySize]);
}
puts("");
return 0;
}
Any more suggestions will be appreciated!
There are a few other problems, but let's talk about your warning. You have this code:
for (arraySize = 0; arraySize < num; arraySize++) {
printf("%ld \n", array[arraySize]);
puts("");
return 0;
}
The corrected indentation should make it obvious why that loop will run at most once.
It seems your print loop has a typo, and should be corrected to:
for (arraySize = 0; arraySize < maxi; arraySize++) {
Also the call to bubble_sort() should use maxi rather than arraySize.
The last character entered gets stored in sort and that character is most probably \n.
Changing scanf("%c", &sort); to scanf(" %c", &sort); should solve the problem.
Note the space before %c.

making a word search puzzle?

I've made a program that allows you to choose the size of the grid and it allows you to enter up to 20 words. Now I have to insert the entered words horizontally into the original array using a function. The function must return a value for success and a value for failure to enter the word into the puzzle board. I need help getting started with what the actual function should look like along with the function prototype. Pseudocode would be helpful. I'm a fairly new programmer so any help is great. Thank you
#include<stdio.h>
#include<string.h>
void printmatrix(char matrix[][20],int);
void inserthor(char matrix[][20],int);
int main(void)
{
//declare variables
char matrix[20][20];
char words[20][100];
int x;
int a,b;
int i=0;
int n=0;
for (a=0;a<20;a++)
{
for (b=0;b<20;b++)
{
matrix[a][b] = '+';
}
}
while (x<10 || x>20)
{
printf("How large would you like the puzzle to be (between 10 and 20):\n");
scanf("%d",&x);
}
printmatrix(matrix,x);
//part 3
printf("Enter up to 20 words to hide in the puzzle.\n");
printf("Enter the word 'done' after your last word if entering less than 20 words.\n");
for (i = 0; i < 20; i++)
{
printf("Enter word %2d:\n", i+1);
if (scanf("%99s", words[i]) != 1 || strcmp(words[i], "done") == 0)
break;
}
n = i;
printf("%d words entered\n", n);
for (i = 0; i < n; i++)
printf("Word %2d = [%s]\n", i+1, words[i]);
return 0;
}
void printmatrix(char matrix[][20],int x)
{
int i,j;
printf("Empty Puzzle:\n");
for (i=0;i<x;i++)
{
for (j=0;j<x;j++)
{
printf(" %c ", matrix[i][j]);
}
printf("\n");
}
}
Your function prototype
void inserthor(char matrix[][20],int);
lacks the parameter with the word to be entered and the value to be returned. You could use
char *inserthor(char matrix[][20], int order, char *word)
{
int i, j, l = strlen(word);
for (i = 0; i < order; ++i)
for (j = 0; j <= order-l; ++j)
if (matrix[i][j] == '+') return memcpy(&matrix[i][j], word, l);
return NULL;
}
which returns the address of the inserted word for success and NULL for failure.

Summing only even numbers in an array

After taking ten numbers as input from the user, I want to add up the ones that are evenly divisible by 2.
I am able to get the input from the user, but I'm not sure how to check which numbers are divisible by two, and add only those.
#include <stdio.h>
#include <ctype.h>
int main(void) {
int i = 0;
int val;
char ch;
int numbers[10];
while(i < 10) {
val = scanf("%d%c", numbers + i, &ch);
if(val != 2 || !isspace(ch)) {
while((ch = getchar()) != '\n') // discard the invalid input
;
printf("Error! Entered number is not an integer.\n");
printf("Please enter an integer again.\n");
continue;
}
++i;
}
printf("%d\n", numbers[0]);
printf("%d\n", numbers[1]);
printf("%d\n", numbers[2]);
printf("%d\n", numbers[3]);
printf("%d\n", numbers[4]);
printf("%d\n", numbers[5]);
printf("%d\n", numbers[6]);
printf("%d\n", numbers[7]);
printf("%d\n", numbers[8]);
printf("%d\n", numbers[9]);
return 0;
}
How about:
int sum = 0;
for (int i = 0; i <= 9; i++)
{
if (numbers[i] % 2 == 0)
sum += numbers[i];
}
completely unnecessary optimization
int sum[2]={0};
for(size_t i = 0; i <=9; ++i) sum[numbers[i]&1]+=numbers[i];
int main(void)
{
int i;
int numbers[10];
int sum = 0;
for(i=0; i<10; ++i)
{
printf("Enter #%d:\n", i+1);
scanf("%d", numbers+i);
if (numbers[i] % 2 == 0) // Then Number is even
{
sum += numbers[i];
}
}
printf("The sum of only the even numbers is %d\n", sum);
getch();
return 0;
}

Resources