Can't run the second task - c

I can't select the second task. If I run the first task, there's no problem, I get the result. But I have some issues in second and third tasks. Even if I select the second or third task, the program asks me to enter an integer, which is used in first task. Here's my code:
#include <stdio.h>
main(void)
{
int n;
int length, row = 0, column = 0, space; // Variables of first program
int top, bot, a, b;
int top2, bot2, a2, b2;
printf("Please select the task: ");
scanf("%d", &n);
if(n = 1)
{
printf("Please enter an integer: "); // Program asks the length
scanf("%d", &length);
row = 0;
column = 0;
while(row < length) // While loop for newline
{
while(column < length) // While loop for stars
{
printf("*");
column++;
}
printf("\n");
column = 0; // Resetting the value of column variable
while(column < row+1) // While loop for space
{
printf(" ");
column++;
}
column = 0;
row++;
}
}
else if(n = 2)
{
printf("Please enter the length of top:");
scanf("%d", &top);
printf("Please enter the length of bottom:");
scanf("%d", &bot);
a = top;
b = top;
while(a <= bot)
{
while(b <= a+2)
{
printf("*");
b++;
}
b = top;
printf("\n");
a++;
}
}
else
{
printf("Please enter the length of top:");
scanf("%d", &top2);
printf("Please enter the length of bottom:");
scanf("%d", &bot2);
a2 = top2;
b2 = top2;
while(a2 >= bot2)
{
while(b2 >= a2)
{
printf("*");
b2--;
}
b2 = top2;
printf("\n");
a2--;
}
}
return 0;
}

if(n = 1)
= is the assign operator.
To compare, use ==.
What you are doing is setting n to 1, and implicitly checking if the value is different than 0. So you always enter the if branch, never the else one.

Related

Create a Menu for users to choose whether they want to add/remove/search for numbers using an array

I want to build a simple user Menu for the user to add/remove and to search for numbers through an array. However, the problem that I am facing is that after successfully running the code and adding numbers to a global array. When choosing the remove option, It showed the list of the array but array1 is not the value of the array I entered, but a total length of the array instead.enter image description here. Please show me which parts that I coded or wrote wrong and excuse me for my bad english! Thank you so much in advance.
#include <stdio.h>
#include <string.h>
int arr[] = {};
int arrLength = 0;
void add(int lengthCheck){
int i,n,check = 0;
do{
printf("\nPlease add a number: ");
scanf("%d",&n);
fflush(stdin);
arr[lengthCheck] = n;
lengthCheck++;
printf("\nNew Array:");
for (i = 0; i < lengthCheck; i++){
printf("\t%d ", arr[i]);
}
printf("\n\n Do you want to continue adding? (y = 0 | n = 1)\n");
scanf("%d", &check);
fflush(stdin);
} while (check < 1);
arrLength = lengthCheck;
}
void remove(int lengthCheck){
int m, i, loop, count = 0;
printf("\nArray:");
for(loop = 0; loop < lengthCheck; loop++){
printf(" %d", arr[loop]);
}
printf("\n\nPlease chooose a number to remove: ");
scanf("%d",&m);
fflush(stdin);
for(loop = 0; loop < lengthCheck; loop++) {
if(arr[loop] == m) {
break;
}
count++;
}
for(i=count; i<lengthCheck - 1; i++)
{
arr[i] = arr[i + 1];
}
lengthCheck--;
printf("\nNew array are : ");
for(i=0; i<lengthCheck; i++)
{
printf("%d\t", arr[i]);
}
}
void search(int arrLength){
int check, loop, t, u;
printf("\nPlease enter a number to check if it exists in the array: ");
scanf("%d",&check);
fflush(stdin);
for(loop = 0; loop < arrLength; loop++) {
if(arr[loop] == check) {
printf("\n\tPosition number %d in the array contains %d\n\n", t,check);
u++;
}
t++;
}
printf("The number %d exist in %d position(s) of the array",check,u);
}
int main(){
int ch, z;
for (z = 0; z < 1;) {
printf("Enter Choice: \n\t(0) for add \n\t(1) for remove \n\t(2) for search\n\t(3) for exit\n");
scanf("%d", &ch);
fflush(stdin);
switch(ch) {
case 0:
add(arrLength);
break;
case 1:
remove(arrLength);
break;
case 2:
search(arrLength);
break;
case 3:
z = 1;
break;
}
}
return 0;
}
you cannot declare an empty array and just add values to it in C.
int arr[] = {};
You need to allocate space for your array upfront, or using malloc to allocate the memory for it later.
This will allocate memory space to store three ints in they array and initialize each element to zero:
int arr[3] = {0};
You will have to check that it never goes above the number of elements declared in the array. If you try to use more space than pre-allocated in the array, you are causing a buffer overflow and all it does is corrupt memory.

Repeat the Program for again Search Array Element

Repeat the Program for again Search Array Element.
#include <stdio.h>
#define MAX_SIZE 100
int main()
{
int arr[MAX_SIZE];
int size, i, toSearch, found;
char repeat;
printf("Enter the size of an array\n");
scanf("%d", &size);
printf("Enter the array elements\n");
for (i = 0; i < size; i++)
{
scanf("%d", &arr[i]);
}
do{
printf("\nEnter element to search: ");
scanf("%d", &toSearch);
found = 0;
for(i=0; i<size; i++)
{
if(arr[i] == toSearch)
{
found = 1;
break;
}
}
if(found == 1)
{
printf("\n%d is found at position %d", toSearch, i + 1);
}
else
{
printf("\n%d is not found in the array \n", toSearch);
}
printf("\n \n \nPress Y to again Search Any Element in Array\n \nPress Any other Key to Exit the Program\n\n");
scanf(" %c \t",&repeat);
}
while(repeat == 'y' || repeat == 'Y' );
return 0;
}
I want to repeat my program when user give the input of Y || y otherwise it'll exit the program.
In this code i want to make an array then search the element after this show's the results and in last repeat the code from the search the element block.
I ran your code and everything seems to be working properly except for this line:
scanf(" %c \t",&repeat);
Remove the \t from the scanf and it should work properly. You don't want to scan for a tab character, just the 'Y' or 'y' character.
Also, your use of newlines is a bit unusual. Try putting newline characters at the end of your strings as opposed to the beginning.
Updated code:
#include <stdio.h>
#define MAX_SIZE 100
int main() {
int arr[MAX_SIZE];
int size, i, toSearch, found;
char repeat = ' ';
printf("Enter the size of an array\n");
scanf("%d", &size);
printf("Enter the array elements\n");
for (i = 0; i < size; i++)
scanf("%d", &arr[i]);
do{
printf("Enter element to search: \n");
scanf("%d", &toSearch);
found = 0;
for(i=0; i<size; i++) {
if(arr[i] == toSearch) {
found = 1;
break;
}
}
if(found == 1)
printf("%d is found at position %d\n", toSearch, i + 1);
else printf("%d is not found in the array\n", toSearch);
printf("Press Y to again Search Any Element in Array\nPress Any other Key to Exit the Program\n");
scanf(" %c",&repeat);
}
while(repeat == 'y' || repeat == 'Y' );
return 0;
}
Enclose the block of code you want to repeat in a while loop, something like
bool flag = false;
while(flag==true) {
//Code block
scanf("%c",&input)
if((input == 'y') || (input == 'Y')) {flag = true;}
else {flag = false;}
}
The first method that came to my mind :
#include <stdio.h>
#define MAX_SIZE 100
int main()
{
int arr[MAX_SIZE];
int size, i, toSearch, found;
char repeat;
printf("Enter the size of an array\n");
scanf("%d", &size);
printf("Enter the array elements\n");
for (i = 0; i < size; i++)
{
scanf("%d", &arr[i]);
}
do
{
printf("\nEnter element to search: ");
scanf("%d", &toSearch);
found = 0;
for (i = 0; i < size; i++)
{
if (arr[i] == toSearch)
{
found = 1;
break;
}
}
if (found == 1)
{
printf("\n%d is found at position %d", toSearch, i + 1);
}
else
{
printf("\n%d is not found in the array \n", toSearch);
}
printf("\n \n \nPress Y to again Search Any Element in Array\n \nPress Any other Key to Exit the Program\n\n");
repeat = getchar();
repeat = getchar();
if(repeat == 'y' || repeat == 'Y') {
continue;
}
else {
break;
}
} while (1);
return 0;
}

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

How to alter my program to use while loops instead of for loops. Asterisk triangle in c

For my programming assignment I have to create 3 programs that print out an asterisk based triangle in c based on the user's input. The difference between the 3 programs would be one will use for loops, the other would use while loops and the last one would use goto. I have the for loop program as well as the goto program, but as for the while loop program I'm not sure how incorporate it into my program. This is my program with a for loop and the second program is my attempt at the while loop version.
#include <stdio.h>
int main() {
int lines, a, b;
//prompt user to input integer
do{
printf("Input a value from 1 to 15: ");
scanf("%d", &lines);
//Check if inputed value is valid
if(lines < 1 || lines > 15) {
printf("Error: Please Enter a Valid number!!!\n");
continue;
}
/*create triangle based on inputed value */
for(a = 1; a <= lines; a++) {
for(b=1; b<= a; b++) {
printf("*");
}
printf("\n");
}
} while(1);
system("pause");
}
Progam #2:
#include <stdio.h>
int main() {
int lines, a = 1, b = 1;
//prompt user to input integer
do{
printf("Input a value from 1 to 15: ");
scanf("%d", &lines);
//Check if inputed value is valid
if(lines < 1 || lines > 15) {
printf("Error: Please Enter a Valid number!!!\n");
continue;
}
while(a <= lines) {
a++;
while (b <= a) {
b++;
printf("*");
}
printf("\n");
}
} while(1);
system("pause");
}
Here is how you convert a for loop like the following
for (stat1; stat2; stat3) {
stat4;
}
to a while loop
stat1;
while (stat2) {
stat4;
stat3;
}
So here is the while loop you want:
a = 1;
while(a <= lines) {
b = 1;
while (b <= a) {
printf("*");
b++;
}
printf("\n");
a++;
}
Set b=1 before 2nd while loop
while(a <= lines) {
a++;
b=1; //you want to start b from 1 for each inner loop
while (b <= a) {
b++;
printf("*");
}
printf("\n");
}
The program2 can be changed as below. The below code result is equivalent to program1.`
#include <stdio.h>
int main() {
int lines, a = 1, b = 1;
//prompt user to input integer
do{
printf("Input a value from 1 to 15: ");
scanf("%d", &lines);
//Check if inputed value is valid
if(lines < 1 || lines > 15) {
printf("Error: Please Enter a Valid number!!!\n");
continue;
}
while(a <= lines) {
//a++;
while (b <= a) {
b++;
printf("*");
}
b =1;
a++1;
printf("\n");
}
} while(1);
system("pause");
}`

make it so when an invalid value is inputted the user must retry a new value in C

For my HW assignment I have to create a program that outputs an asterisk based triangle that depends on user input. I have gotten my program to work as far as when the user inputs an integer the correct triangle is outputted, but my issue is when an invalid value is inputted how do I make it so that the user must re-attempt to submit a value? I looked on the forums and I have not been able to find a similar question.
#include <stdio.h>
int main() {
int lines, a, b;
//prompt user to input integer
printf("Input a value from 1 to 15: ");
scanf("%d", &lines);
//Check if inputed value is valid
if(lines >= 1 && lines <= 15) {
/*create triangle based on inputed value */
for(a = 1; a <= lines; a++) {
for(b=1; b<= a; b++) {
printf("*");
}
printf("\n");
}
}
else {
printf("not valid");/* repeat code in this else statement, maybe */
}
system("pause");
}
#include <stdio.h>
int main() {
int lines, a, b;
//prompt user to input integer
do{
printf("Input a value from 1 to 15: ");
scanf("%d", &lines);
//Check if inputed value is valid
if(lines < 1 || lines > 15) {
printf("Error: Please Enter a Valid number!!!\n");
continue;
}
/*create triangle based on inputed value */
for(a = 1; a <= lines; a++) {
for(b=1; b<= a; b++) {
printf("*");
}
printf("\n");
}
}while(1);
system("pause");
}
If you want to stop program if user enter a Valid value(I mean 1-15) then put these for loops in else block and add break statement.
do{
printf("Input a value from 1 to 15: ");
scanf("%d", &lines);
//Check if inputed value is valid
if(lines < 1 || lines > 15) {
printf("Error: Please Enter a Valid number!!!\n");
continue;
}
else{
/*create triangle based on inputed value */
for(a = 1; a <= lines; a++) {
for(b=1; b<= a; b++) {
printf("*");
}
printf("\n");
}
break;
}
}while(1);
system("pause");
}
You can use do .. while loop to ask user for valid input. Code
int main() {
int lines, a, b;
do {
//prompt user to input integer
printf("Input a value from 1 to 15: ");
scanf("%d", &lines);
//Check if inputed value is valid
if(lines >= 1 && lines <= 15) {
/*create triangle based on inputed value */
for(a = 1; a <= lines; a++) {
for(b=1; b<= a; b++) {
printf("*");
}
printf("\n");
}
break; //break while loop after valid input
}
else {
printf("not valid");/* repeat code in this else statement, maybe */
}
}while(1);
system("pause");
}

Resources