I'm trying to learn about pointer, and I got a task, but when I tried to print a float array, the 1st value changed.
void getinputA(double *aa){
while(1){
scanf("%lf", &*aa);
getchar();
if(*aa< 100 || *aa>10000){
printf("input the right amount\n")
}else{
printf("success\n\n");
break;
}
}
return;
}
void getinputB(double *bb){
while(1){
scanf("%lf", &*bb);
getchar();
if(*ten == 1 || *ten == 3 || *ten == 6 || *ten == 12){
printf("success\n\n")
}else{
printf("input the right number\n");
break;
}
}
return;
}
void getinputC(double *cc){
while(1){
scanf("%lf", &*cc);
getchar();
if(*cc<1 || *aa>10){
printf("input the right number\n")
}else{
printf("success\n\n");
break;
}
}
return;
}
int main(){
double A;
int B, C, Rep, D;
getinputA(&A);
Rep = (12/B)*C
for(int i=0; i<Rep; i++){
if(i == 0){
D[i] = A;
//checked, still correct
}else{
D[i] = AnotherVarInMyCode[i-1];
//checked, still correct
}
AnotherVar[i] = A[i]*(DifferentVar/(12/B));
AnotherVarInMyCode[i] = A[i]+AnotherVar[i];
}
AFunction();
//here
for(int i=0; i<Rep; i++){
printf("%d %.2lf\n", i+1, D[i]);
//when I input 1000, it prints -400, and when I input 100, it -0,98
}
return 0;
}
I found out that the value change after it runs a function which I don't use D, why does this happen? And how to fix it?
I've checked my code, but I can't figure out why it changed.
if changed from 1000 to -200, and from 100 to -0.98
Related
#include <stdio.h>
int main(void)
{
int a;
scanf("%d", &a);
for (int i = 0; ; i++)
{
char values[2];
scanf("%c", &values[0]);
scanf("%d", &values[1]);
if (values[0] == '*')
{
a = a * values[1];
printf(" = %d\n", a);
}
else if (values[0] == '+')
{
a = a + values[1];
printf(" = %d\n", a);
}
else if (values[0] == '%')
{
a = a % values[1];
break;
}
}
printf("%d", a);
}
When I input 5 + 3 + 7 + 10 + 2 + 3 + 1 % 11, it would show 5 (because 5%11 = 5). But the + operation didn't work. Can you see what is the problem here?
I think as values[2] is only two variable you need, you can use two different variable to do your job. use one char type variable and one int type variable as you need these two. You have another problem in your code, use a getchar() in the inside of the loop then your code will work fine, cause when you give a integer value as input then take a character value last you enter the new line that goes to that character that is why your code was giving error.
#include <stdio.h>
int main(void){
int a;
scanf("%d", &a);
for (int i = 0; ; i++)
{
getchar();
char ch;
int value;
scanf("%c", &ch);
scanf("%d", &value);
if (ch == '*')
{
a = a * value;
printf(" = %d\n", a);
}
else if (ch == '+')
{
a = a + value;
printf(" = %d\n", a);
}
else if (ch == '%')
{
a = a % value;
break;
}
}
printf("%d", a);
}
There are multiple problems:
value is an array of char: scanning the operator into a char is fine, but the value should be converted into an int variable.
the scanf("%c", ...) will store the next byte into the variable, but after converting an int, the next byte is the pending space or newline, not the '+'. You should use scanf(" %c", ...) to skip the whitespace after the previous conversion.
Here is a modified version:
#include <stdio.h>
int main(void) {
int a;
if (scanf("%d", &a) != 1)
return 1;
for (int i = 0; ; i++) {
char op;
int value;
if (scanf(" %c", &op) != 1)
break;
if (scanf("%d", &value) != 1)
break;
if (op == '*') {
a = a * value;
printf(" = %d\n", a);
} else
if (op == '+') {
a = a + value;
printf(" = %d\n", a);
} else
if (op == '-') {
a = a - value;
printf(" = %d\n", a);
} else
if (op == '%') {
a = a % value;
printf(" = %d\n", a);
} else
if (op == '/') {
a = a / value;
printf(" = %d\n", a);
} else {
printf("invalid operator %c\n", op);
break;
}
}
printf("%d\n", a);
return 0;
}
I have new problem about while-loop!
When I input the l_res as y it means that the condition of while loop is still TRUE, instead repeat it from the start, it terminated the program. → enter image description here
int main()
{
//VARIABLES
char f_res[4];
char l_res = 'y';
int a,b,c,x;
while(l_res == 'y')
{
printf("Enter 3 digit capacitor Code: ");
scanf("%d %d %d",&a,&b,&c);
x = (a*10)+b;
printf("What is the unit of the value?(Pico-picofarad, Nano-Nanofarad): ");
scanf("%s", f_res);
if(strcmp(f_res, "Pico") == 0)
{
if(c == 0)
{
x = x;
}
else
{
while(c <= 5 && c > 0)
{
c--;
x = x*10;
}
}
printf("\nCapacitor value is: %d pF\n\n",x);
}
else if(strcmp(f_res, "Nano") == 0)
{
float x = ((a*10)+b)*0.001;
if(c == 0)
{
x = x;
}
else
{
while(c <= 5 && c > 0)
{
c--;
x = x*10;
}
}
printf("\nCapacitor value is: %.3f pF\n\n",x);
}
else
{
printf("\nINVALID Input!\n\n");
}
printf("Do you want to enter other capacitor Code?( y-Yes, n-No): ");
scanf(" %c", l_res);
}
return 0;
}
How can I fix this?
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;
}
I need a program that asks for an integer &prints out an emtpy asterisk square based on the input number. And if the input was invalid then i need it to try and ask again
.
THIS is what i have so far (it already can make a square based on a variable number) i just need to enhence it so it asks again if the user is too dumb to input an integer
#include <stdio.h>
int Loops() {
int s, e, z;
printf("Input number \n");
scanf("%d", &s);
for(e = 0; e < s; e++) {
for(z = 0; z < s; z++) {
if(e==0 || e==s-1 || z==0 || z==s-1)
printf("*");
else
printf(" ");
}
printf("\n");
}
}
int main() {
Loops();
}
i think the teacher mentioned something of adding a switch case...?
I propose this solution, in a few words I check the type of number entered by the user, if it's >0 then run,you do not have to worry that it is float because you have defined an integer type and the part not integer is automatically troncated.
#include <stdio.h>
int Loops() {
int s, e, z;
printf("Input number \n");
scanf("%d", &s);
if (s > 0 ) {
for (e = 0; e < s; e++) {
for (z = 0; z < s; z++) {
if(e == 0 || e == s - 1 || z == 0 || z == s - 1)
printf("*");
else
printf(" ");
}
printf("\n");
}
}
else
Loops();
}
int main() {
Loops();
return 0;
}
You can simply use an infinite loop i.e. while(1) loop with a condition to break. Let's say you want a user to input positive integer and discard negative numbers and alphabets as well, you can do that in the following manner:
int Loops() {
int s, e, z;
while(1)
{
printf("Input number \n");
e = scanf("%d", &s);
if((s <= 0) || (e != 1))
{
printf("Please Enter Non-negaitve Number\n");
getchar(); //This is to consume the '\n'
}
else
{
break;
}
}
for(e = 0; e < s; e++) {
for(z = 0; z < s; z++) {
if(e==0 || e==s-1 || z==0 || z==s-1)
printf("*");
else
printf(" ");
}
printf("\n");
}
}
I have written a program in C, that when runs, is supposed to allow the user to play a game of tic tac toe - or noughts and crosses, whether it be against the computer, or another user. I have nearly completed it, and it prints the grid out perfectly fine. However, when I go to input which row/column I want to place the symbol in, instead of inserting the symbol, it just removes a space in the grid, aligning it incorrectly. I have tried to put a loop in the main method which fills the grid in with spaces to try and prevent null values.
If anyone could suggest anything it would be much appreciated. Thanks in advance!
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void user_prompt();
void clear_board(char gameBoard[3][3]);
void display_board(char gameboard[3][3]);
void user_move(char gameBoard[3][3]);
void computer_move(char gameBoard[3][3]);
int detect_win(char gameBoard[3][3]);
int gameMode;
char symbol1;
char symbol2;
char nickname1[10];
char nickname2[10] = "TicTacBot";
char gameBoard[3][3];
char playerTurn[10];
int main(void){
int i, j;
char gameBoard [3][3];
clear_board(gameBoard);
for(int i = 0; i < 3; i++) {
for(int j = 0; j < 3; j++) {
gameBoard[i][j] = ' ';
}
}
i = 0;
j = 0;
user_prompt();
display_board(gameBoard);
if(gameMode == 2){
for(i = 0; i < 9; i++){
user_move(gameBoard);
display_board(gameBoard);
if(detect_win(gameBoard) == 1){
printf("The winner is %s!!!\n", nickname1);
}
if(detect_win(gameBoard) == 2){
printf("The winner is %s!!!\n", nickname2);
}
}
}
else{
user_move(gameBoard);
display_board(gameBoard);
for(i = 0; i < 4; i++){
printf("TicTacBot's move.\n");
computer_move(gameBoard);
display_board(gameBoard);
if(detect_win(gameBoard) == 1){
printf("The winner is %s!!!\n", nickname1);
}
user_move(gameBoard);
display_board(gameBoard);
if(detect_win(gameBoard) == 1){
printf("The winner is %s!!!\n", nickname2);
}
}
}
if(detect_win(gameBoard) == 0){
("The game ended in stalemate!\n");
}
return 0;
}
/*
Prompts the user to enter a nickname, what symbol they would like to use, and whether they'd rather play against the computer or another user.
*/
void user_prompt(void){
printf("Please choose whether you would rather play against the computer (enter '1'),or another user (enter '2'): \n");
scanf("%d", &gameMode);
getchar();
while(gameMode != 1 && gameMode != 2){
printf("Please enter a valid digit: \n");
scanf("%d", &gameMode);
getchar();
}
if(gameMode == 1){
printf("Please choose whether you would rather play as 'X' (enter 'x') or as 'O' (enter 'o'): \n");
scanf(" %c", &symbol1);
getchar();
if(symbol1 == 'x') {
symbol2 == 'o';
}
else{
symbol2 == 'x';
}
}
else{
printf("Player 1, would you like to play as 'X' (enter 'x') or as 'O' (enter 'o'): \n");
scanf(" %c", &symbol1);
getchar();
while(symbol1 != 'x' && symbol1 != 'o'){
printf("Please enter a valid symbol: \n");
scanf(" %c", &symbol1);
getchar();
}
if(symbol1 == 'x'){
symbol2 == 'o';
}
else{
symbol2 == 'x';
}
}
if(gameMode == 1){
printf("Please enter a nickname: \n");
fgets(nickname1, 10, stdin);
getchar();
}
else{
printf("Please enter a nickname for player 1: \n");
fgets(nickname1, 10, stdin);
getchar();
printf("Please enter a nickname for player 2: \n");
fgets(nickname2, 10, stdin);
getchar();
}
}
/*
Resets the game board.
*/
void clear_board(char gameBoard[3][3]){
for(int i = 0; i < 3; i++){
for(int j = 0; j < 3; j++){
gameBoard[i][j] = 0;
}
}
}
/*
Displays the game board and all symbols within it.
*/
void display_board(char gameBoard[3][3]){
for(int i = 0; i < 3; i++){
for(int j = 0; j < 3; j++){
printf(" %c ", gameBoard[i][j]);
if(j == 2){
printf("\n");
}
if(j != 2){
printf("|");
}
}
if(i != 2){
printf("---+---+---\n");
}
}
}
/*
Takes input of a position on the board from the user, then places the user's symbol into that space on the game board.
*/
void user_move(char gameBoard[3][3]){
int row;
int column;
if(playerTurn == nickname1){
printf("Would you like to enter your %c in row 1, 2 or 3? \n", symbol1);
scanf("%d", &row);
getchar();
printf("Would you like to enter your %c in column 1, 2 or 3? \n", symbol1);
scanf("%d", &column);
getchar();
}
else{
printf("Would you like to enter your %c in row 1, 2 or 3? \n", symbol2);
scanf("%d", &row);
getchar();
printf("Would you like to enter your %c in column 1, 2 or 3? \n", symbol2);
scanf("%d", &column);
getchar();
}
if(row < 1 || row > 3){
printf("Please enter a valid row number: \n");
scanf("%d", &row);
getchar();
}
if(column < 1 || column > 3){
printf("Please enter a valid column number: \n");
scanf("%d", &row);
getchar();
}
if(gameBoard[row-1][column-1] != ' '){
printf("The position you entered is already taken. Try again! \n");
display_board(gameBoard);
user_move(gameBoard);
}
else if(gameBoard[row-1][column-1] != ' '){
printf("The position you entered is already taken. Try again! \n");
display_board(gameBoard);
user_move(gameBoard);
}
else{
if(playerTurn == nickname1){
gameBoard[row-1][column-1] = symbol1;
}
else{
gameBoard[row-1][column-1] = symbol2;
}
}
printf("%c", symbol2);
if(gameMode == 2){
return;
}
if(strcmp(playerTurn, nickname1)==0){
strcpy(playerTurn, nickname2);
}
else if(strcmp(playerTurn, nickname2)==0){
strcpy(playerTurn, nickname1);
}
}
/*
Automates a strategic move from the computer, aiming to win the game, or at the least prevent the user from winning
*/
void computer_move(char gameBoard[3][3]){
int row;
int column;
int endTurn = 0;
row = rand() % 3 + 0;
column = rand() % 3 + 0;
if(gameBoard[row][column] != symbol1 && gameBoard[row][column] != symbol2){
gameBoard[row][column] = symbol2;
endTurn = 1;
}
}
/*
Detects a win on the game board. Checks if there are three identical symbols in a row, or if there are no more spaces on the game board.
*/
int detect_win(char gameBoard[3][3]){
for(int row = 0; row < 3; row++){
if(gameBoard[row][0] == gameBoard[row][1] && gameBoard[row][1] == gameBoard[row][2]){
if(gameBoard[row][0] == symbol1){
return 1;
}
if(gameBoard[row][0] == symbol2){
return 2;
}
}
}
for(int column = 0; column < 3; column++){
if(gameBoard[0][column] == gameBoard[1][column] && gameBoard[1][column] == gameBoard[2][column]){
if(gameBoard[0][column] == symbol1){
return 1;
}
if(gameBoard[0][column] == symbol2){
return 2;
}
}
}
if(gameBoard[0][0] == gameBoard[1][1] && gameBoard[1][1] == gameBoard[2][2]){
if(gameBoard[1][1] == symbol1){
return 1;
}
if(gameBoard[1][1] == symbol2){
return 2;
}
}
if(gameBoard[0][2] == gameBoard [1][1] && gameBoard[1][1] == gameBoard[2][0]){
if(gameBoard[1][1] == symbol1){
return 1;
}
if(gameBoard[1][1] == symbol2){
return 2;
}
}
return 0;
}
There's a small logical error:
In line 98, 101, 117, and 121, (and perhaps other places - please check) you have used == (operator for equality check) instead of the = used for assignment.
For example,
symbol2 == 'o';
should be replaced with
symbol2 = 'o';
Hence, it just checks for equality, throws away the result, and continues; with no changes made to symbol2.