Replacing already printed numbers in C - c

I am writing a program in C language which is an India Game called Thambola(similar to Bingo).
In this game the user gets a 3x9 ticket and the computer asks the user to enter the number which the second program(this picks numbers randomly from 1-90) picked. If the entered number exists in the ticket, the number should be changed to 'x' meaning the number has been stricken off. I need help here. How to replace an already printed number with 'x'? i read this C - Remove and replace printed items but it doest help because i have 27 numbers to be changed.Please help me. Here is the part of the code:
int number,i,j;
const char x='x';
printf("\nEnter the number:");
scanf("%d",&number); // number entered by the user from the Picker
for (i=0;i<3;i++)
for (j=0;j<9;j++)
{if (ticket[i][j]==number)
ticket[i][j]=x;
printf("%d",ticket[i][j]); //if the input number is present in the ticket, this should change it to 'x
}
getchar();
}

Main problem of your code snippet is your logic behind assigning values to variables.
As anyone can see, you have an array of integers but you want to assign a char to one of its elements. Actually you can do it by casting, but the result will be the ASCII value of the 'x'.
{if (ticket[i][j]==number) // number is an integer
ticket[i][j]=x; // x is a char
Since 'x' has the ASCII value of 120,which is out of range from the possible numbers in the ticket, you can safely cast 'x' to its integer value and then assign it to its array element. In printing, if you see 120 print 'x'.
In the situations where your char's integer value is in the range of the other possible integer values, pick another integer value and treat this value as 'x' in logic flow (for example, pick 0 for the corresponding integer, if 0 comes print 'x').
You can use ncurses or simply print a ticket state then clear the screen just before printing modified ticket for output.

public class sample1 {
public static void main(String[] args) {
// TODO Auto-generated method stub
List<int[]> outer = new ArrayList<int[]>(9);
List<int[]> inner = new ArrayList<int[]>(9);
int[][] final_arr = new int[9][18];
int[][][] final_arr2 = new int[6][3][9];
int[][] multi = new int[][]{
{2,3,5,7,9,12,13,15,17},
{1,3,4,5,6,7,11,14,16,18},
{1,2,4,6,8,9,12,13,17,18},
{3,5,7,9,10,11,12,14,15,16},
{1,4,6,7,8,10,11,13,14,18},
{2,3,5,8,9,12,13,15,16,17},
{1,2,4,6,7,10,11,13,17,18},
{1,3,5,8,9,12,13,15,16,18},
{2,4,6,7,9,10,11,14,15,16,17}
};
for( int i=0;i<9;i++){
outer.add(multi[i]);
}
for(int x=0;x<9;x++){
int [] temp = new int[18];
for(int k=0;k<outer.get(x).length;k++){
//System.out.print(outer.get(x)[k]-1 +" ");
int row = outer.get(x)[k]-1;
temp[row]=1;
}
//System.out.println();
inner.add(temp);
}
System.out.println();
int count=1;
for(int i=0;i<9;i++){
List<Integer> temp = new ArrayList<Integer>();
for(int k=0;k<outer.get(i).length;k++){
temp.add(count);
count++;
}
Collections.shuffle(temp);
int index_of_temp=0;
for(int j=0;j<18;j++){
if(inner.get(i)[j]==1){
inner.get(i)[j]=temp.get(index_of_temp);
index_of_temp++;
}
//System.out.print(inner.get(i)[j]+ " ");
}
//System.out.println();
}
for(int i=0;i<9;i++){
for(int j=0;j<18;j++){
final_arr[i][j]=inner.get(i)[j];
}
}
System.out.println();
for(int i=0;i<18;i++){
for(int j=0;j<9;j++){
//System.out.print(final_arr[j][i]+ " ");
final_arr2[i/3][i%3][j]=final_arr[j][i];
}
}
for(int k=0;k<6;k++){
for(int i=0;i<3;i++){
for(int j=0;j<9;j++){
System.out.print(final_arr2[k][i][j]+ " ");
}
System.out.println();
}
System.out.println();
}
}
}

Related

How can I check if a user input of 2D Array contains a given user input integer value? If found, how can I print its index?

For example, a given user input array is {{1,2,3},{4,5,6}} and the user input integer value is 6. If 6 is in the array, I want to print its index (for example [1][2]). I was able to write the code that will scan the given elements of the array however, I'm not sure what to do next in order to find the given integer value in the array.
Here's my code:
import java.util.Scanner;
public class Main{
public static void main(String args[]){
Scanner scan = new Scanner(System.in);
int toFind;
System.out.print("Enter the number of rows: ");
int row =scan.nextInt();
System.out.print("Enter the number of columns: ");
int column =scan.nextInt();
int input[][] = new int[row][column];
System.out.println("Elements: ");
for(int ctr1 = 0; ctr1 < input.length; ctr1++)
{
for(int ctr2 = 0; ctr2 < input[ctr1].length; ctr2++)
{
input[ctr1][ctr2] = scan.nextInt();
}
}
System.out.println("Enter the number you want to find: ");
toFind = scan.nextInt();
I'm not sure what to next here in order to initiate the checking of the number in the given array and print its index.

C Programming How to get results of an mathematical operation stored in a different array?

I am trying to subtract a given number from an array and then store the results in a completely different array. Is it possible to write the code without using pointers?
I am trying to write the code with using for loop and or do/while loop.
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
int main(){
int num[100];
int i ;
int size;
int sub;
int diff[100];
printf("Enter the size of the array: ");
scanf("%d", &size);
for(i=0;i<size; i++){
printf("Enter the element %d :", i+1);
scanf("%d", &num[i]);
}
printf(" Enter the number to substract: \n");
scanf("%d", &sub);
for (i=0;i<size; i++)
{
y = num[i]- sub;
scanf("%d", &diff[y]);
}
for (y=0; y<size; y++)
{
printf("%d", diff[y]);
}
}
After I scan the results, I tried different ways to initialize and store the values in the second array but haven't been successful. What mistake am I making here?
y = num[i] - sub;
This is fine, as it's the result of subtraction for a given source array element.
scanf("%d", &diff[y]);
This doesn't make sense, as it's attempting to read input from the user. Not only that, it's using the result of the subtraction as the index of the destination array.
Just assign the result of the subtraction to the corresponding destination array member:
diff[i] = num[i] - sub;
In your question, you try to scan the value to another array, but the correct form is to assign the value in the new array position.
For example, in your first for loop use the i variable as the position and assign num[i] - sub on diff[i]:
for (i = 0; i < size; i++)
{
diff[i] = num[i] - sub;
}
instead of:
for (i=0;i<size; i++)
{
y = num[i]- sub;
scanf("%d", &diff[y]);
}

Store a given amount of numbers using pointers in C

I'm trying to learn how to use pointers, and I'm trying to make a program that asks the user for a random number of integers they'd like to write in, and then printing them back to the user. Normally I'd use an array for this, but that defeats the whole purpose of learning pointers.
#include <stdio.h>
#include <malloc.h>
int main() {
int numberAmount = 0;
int *numbers;
printf("Type the amount of numbers you are going to write: ");
scanf("%i", &numberAmount);
numbers = (int*) malloc(sizeof(numberAmount));
if (numberAmount == 0) {
printf("No numbers were given");
}
else {
for (int i = 0; i < numberAmount; i++) {
scanf("%i", numbers);
}
while (*numbers != 0) {
printf("%i ", *numbers);
numbers++;
}
}
return 0;
}
This is what I've come up with so far, but it does not work.
Any ideas?
In this part of your code
for (int i = 0; i < numberAmount; i++) {
scanf("%i", numbers);
}
you're saving the number that the user inputted in the same memory location. So the value saved in the numbers pointer keeps changing whenever the user inputs a new integer instead of adding a new integer.
You can fix this by replacing scanf("%i", numbers); with scanf("%i", (numbers + i));. This way for every new input the user provides, it will be saved in the memory location next to numbers.

How to input a 2-D array directly in C with negative values, spaces and newline

I want to directly input a 2-D Array in C, separated just by single spaces and newlines. At the same time I also want to verify whether the user is entering a valid single digit integer (either positive or negative).
I tried the following.
int A[3][3];
int i,j;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
scanf("%d",&A[i][j]);
}
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
printf("%d ",A[i][j]);
printf("\n");
}
But I want 2 things in this:-
Input that is first verified whether its a single digit integer or not.
Not works merely for positive integers and 0 but also for negative integers.
eg.
input such as the following should be accepted and stored in 2-d array
1 2 -3 4
-5 1 2 -6
1 1 2 3
I'm sorry if I wasn't clear. An important component is that the input should be confirmed whether its integer or not i.e. the input should be either as char or string and if it is an integer (which can be ascertained by functions such as isdigit() , then it should be converted to an integer value.
This following code chunk works for single positive values
char c = getchar();
if (isdigit(c))
int value = c - '0';
However, I don't know how to enable this functionality for negative integers in a complete 2D array input.
If you want to verify that a number is a single digit, just check if its between -9 and 9.
As for negative numbers, im pretty sure the %d modifier for the scanf captures that.
Please comment down below if the answer is incorrect so i could fix it :)
Edit: i forgot to note that scanf returns the number of read elements. so comparing this against 1 (since you read one element at a time) will allow you to know if the input was partial or not.
something like this:
if (scanf(" %d", &A[i][j]) != 1){
//here goes the code for when the input is partial
}
should do the trick
So first, tell the user to enter values row-wise or column-wise. In this case, ask like this
printf("Enter integers row-wise");
Now the user will enter values.
After scanning those values put an if statement to verify that value is greater than or equal to -9 and less than 9. If value is within the range continue; else put a exit(1); statement and tell the user that entered value is incorrect. Like this:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int A[3][3];
int i, j;
for( i = 0; i < 3; i++ )
{
for( j = 0; j < 3; j++)
{
scanf("%d", &A[i][j]);
if(A[i][j] >= -9 && A[i][j] <= 9)
continue;
else
{
printf("Enter correct values.\n");
exit(1);
}
}
}
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
printf("%d ",A[i][j]);
printf("\n");
}
return 0;
}

for loop will perform only one loop

I need to make a C program to read the number of students(1<=students<=25) in a class and for every student to read his exam score ex 10/20 15/20 etc(1<=score<=20) and print the max and the max score of students and the average score of class.
I made the program but it performs the for loop only once for some reason.
Can you please help me understand why?
here is the code :
#include <stdio.h>
int main(void) {
int m,i,b,sum,min,max,mo;
sum=0;
while (m<1 || m>25) {
printf("give number of students ");
scanf("%d",&m);
}
for (i=1; i<(m+1); i++) {
while (b<1 || b>20) {
printf("give score of %d student",i);
scanf("%d",&b);
}
if(i==1) {
min=b;
max=b;
}
else {
if(b<min) min=b;
if(b>max) max=b;
}
sum=sum+b;
}
mo=sum/m;
printf("max is %d and min is %d and avg is %d",max,min,mo);
}
First - initialize your variables:
int m,i,b,sum,min,max,mo; // these are declared and uninitialized
m = 0; // now it's initialized to 0
i = 0;
...
If you don't initialize them to something, you don't know what they are to start with.
Second - You need to change the value of b:
for (i=1; i<(m+1); i++) {
while (b<1 || b>20) { <-- here you're checking for b being valid
printf("give score of %d student",i);
scanf("%d",&b);
}
So the first time in b will be between 1 and 20, if you don't reset it to something invalid you'll never get here again. After you record the value of b:
sum=sum+b;
b = 0; // we're done with b for now, set it to something invalid for the while()
}
Not initialized the m and using in while condition
It is undefined behaviour using uninitialized local variable in conditon
Want to implement the same
use
do
{
printf("give number of students ");
scanf("%d",&m);
}
while(m<1 || m>25);
for (i=1; i<(m+1); i++) change the condition as i<=m It's the good technique rather < and then adding 1
Inside this loop use the same do while loop
You forgot to initialize the B variabkle, so it loops m times, never asking you to insert a score!
1. You initialize 'm' here, without any prior value
int m,i,b,sum,min,max,mo;
2. Without a value, you check for this condition. Which means, a garbage value would be used. (May/May not fulfill your condition)
while (m<1 || m>25) {
3. The crucial scanf for m is inside the previous while. Without which your FOR would run for a basic i=1 and stop.
for (i=1; i<(m+1); i++) {
You need to understand about Garbage Values in C and vital step of initializing a variable to an initial value before using it.
You may read more on this link:
What is a garbage value/How does it occur in C

Resources