is there any way I convert this program to using structures? I would like to do that but I have no idea how, but I know its possible. I am just begginer in this soI will be thankful for any kind of help. Thank you
#include <stdio.h>
int main(void)
{
int pole[100];
int x, i, j, q, min[100], max[100], min_cislo, max_cislo, min_pocet = 0, max_pocet = 0;
printf("Napis, kolko cisel chces ulozit do pola :");
scanf("%d",&x);
printf("Vloz %d cisla do pola :\n",x);
for(i=0;i<x;i++){
printf("cislo %d -: ",i);
scanf("%d",&pole[i]);
if(i==0){
min_cislo = pole[i];
max_cislo = pole[i];
min[min_pocet++] = i;
max[max_pocet++] = i;
continue;
}
if(pole[i] <= min_cislo){
if(pole[i] == min_cislo){
min[min_pocet++] = i;
}else{
min_pocet = 0;
min_cislo = pole[i];
min[min_pocet++] = i;
}
}
if(pole[i] >= max_cislo){
if(pole[i] == max_cislo){
max[max_pocet++] = i;
}else{
max_pocet = 0;
max_cislo = pole[i];
max[max_pocet++] = i;
}
}
}
printf("min cislo bolo: %d, na indexoch:", min_cislo);
for(j = 0; j < min_pocet; j++){
printf(" %d,", min[j]);
}
printf("\n");
printf("max cislo bolo: %d, na indexoch:", max_cislo);
for(q = 0; q < max_pocet; q++){
printf(" %d,", max[q]);
}
printf("\n");
return 0;
}
Okay I figured it your like this because the quest was to save value and position of numbers into struct. see my code:
#include <stdio.h>
struct {
int min_hodnota;
int max_hodnota;
int min_pozicia[100];
int max_pozicia[100];
} bod;
int main(void)
{
int pole[100];
int x, i, j, q , min_pocet = 0, max_pocet = 0;
printf("Napis, kolko cisel chces ulozit do pola :");
scanf("%d",&x);
printf("Vloz %d cisla do pola :\n",x);
for(i=0;i<x;i++){
printf("cislo %d -: ",i);
scanf("%d",&pole[i]);
if(i==0){
bod.min_hodnota = pole[i];
bod.max_hodnota = pole[i];
bod.min_pozicia[min_pocet++] = i;
bod.max_pozicia[max_pocet++] = i;
continue;
}
if(pole[i] <= bod.min_hodnota){
if(pole[i] == bod.min_hodnota){
bod.min_pozicia[min_pocet++] = i;
}else{
min_pocet = 0;
bod.min_hodnota = pole[i];
bod.min_pozicia[min_pocet++] = i;
}
}
if(pole[i] >= bod.max_hodnota){
if(pole[i] == bod.max_hodnota){
bod.max_pozicia[max_pocet++] = i;
}else{
max_pocet = 0;
bod.max_hodnota = pole[i];
bod.max_pozicia[max_pocet++] = i;
}
}
}
printf("min cislo bolo: %d, na indexoch:", bod.min_hodnota);
for(j = 0; j < min_pocet; j++){
printf(" %d,", bod.min_pozicia[j]);
}
printf("\n");
printf("max cislo bolo: %d, na indexoch:", bod.max_hodnota);
for(q = 0; q < max_pocet; q++){
printf(" %d,", bod.max_pozicia[q]);
}
printf("\n");
return 0;
}
I guess you want to build a struct whose fields are the bunch of variables you have defined at the beginning. Well, as pointed by #"L. Scott Johnson", you don't have to. But if you want anyways you jus have to define a struct like this:
struct {
int x;
int i;
int j;
int min[100];
}example;
and just use the struct like this:
example.i=4;
example.min={1,4,5,3,4,8,99,123... };
etc.
Related
I have to write a program in C which, prints numbers from collatz as a triangle.
Like this:
I have tried and
This is my output:
This is my code:
int inputLines, startNum, number;
printf("Lines: ");
scanf("%d",& inputLines);
printf("Start: ");
scanf("%d",& startNum);
for(int i = 0; i < inputLines; i++){
printf("\n");
for(int j = 0; j <= i; j++){
printf("%d ", startNum);
if(startNum % 2 == 0){
startNum = startNum / 2;
}else{
startNum = startNum * 3 + 1;
}
}
}
#include <stdio.h>
int main(void) {
int inputLines = 4;
int start = 19;
for(int i = 0; i < inputLines; i++)
{
int n = start;
for(int j=0; j<i+1; ++j)
{
printf("%d ", n);
n = (n%2)? 3*n+1 : n/2;
}
printf("\n");
}
return 0;
}
The program is about finding the average temperature of all cities, but the result doesn't make sense (it is wrong)
I have tried splitting the program into functions for better understanding, but that didn't help me much.
#define amount_cities 5
#define amount_temp 3 // temp = temperature
int mintemp(int mas[amount_cities][amount_temp], int *index);
int maxtemp(int mas[amount_cities][amount_temp], int *index);
float avgtemp(int mas[amount_cities][amount_temp]);
int main(){
int arr_temp[amount_cities][amount_temp];
int i, j, ind;
// Set a temperatures for each city
for(i = 0; i < amount_cities; ++i){
printf("Temperature - city %d \n", i);
for(j = 0; j < amount_temp; ++j){
printf("Temperature %d - ", j+1);
scanf("%d", &arr_temp[i][j]);
}
}
for(i = 0; i < amount_cities; ++i){
printf("Temperature - city %ds \n", i);
for(j = 0; j < 3; ++j){
printf(" %d.- %d", j+1, arr_temp[i][j]);
}
printf("\n");
}
printf("Minimal temperature = %d ", mintemp(arr_temp, &ind));
printf("for city %d \n", ind);
printf("Maximal temperature = %d ", maxtemp(arr_temp, &ind));
printf("City %d \n", ind);
printf("Average temperature = %.2f\n", avgtemp(arr_temp));
return 0;
}
int mintemp(int mas[amount_cities][amount_temp], int *index){
int m, n, min_t;
min_t = mas[0][0];
*index = 0;
for(m = 0; m < amount_cities; ++m){
for(n = 0; n < amount_temp; ++n){
if(mas[m][n] < min_t){
min_t = mas[m][n];
*index = m;
}
}
}
return min_t;
}
int maxtemp(int mas[amount_cities][amount_temp], int *index){
int m, n, max_t;
max_t = mas[0][0];
for(m = 0; m < amount_cities; ++m){
for(n = 0; n < amount_temp; ++n){
if(mas[m][n] > max_t){
max_t = mas[m][n];
*index = m;
}
}
}
return max_t;
}
float avgtemp(int mas[amount_cities][amount_temp]){
int m, n;
float average_t = 0.0; // Average temperature
for(m = 0; m < amount_cities; ++m){
for(n = 0; n < amount_temp; ++n){
average_t += mas[m][n];
}
}
average_t /= amount_cities*mas[m][n];
return average_t;
}
I expected the output to be , 33.00 but got Average temperature = 30.00 instead.
When i run this code, it print everytime 0.000000 instead of something plausible:
#include <stdio.h>
#include <time.h>
#define n 10
void ordinaVett(int v[], int elem){
clock_t inizio, fine;
double tempo;
int i, j, x, posmin, tmp;
printf("\n\nOrdinamento vettore di %d elementi per %d volte: \n", elem, n);
printf("Sele-sort:\n");
for(x = 0; x < n; x++){
inizio = clock();
//Ordinamento con Sele-sort
for(i = 0; i < elem - 1; i++){
posmin = i;
for(j = i + 1; j < elem; j++){
if(v[j] < v[posmin])
posmin = j;
}
if(posmin != i){
tmp = v[i];
v[i] = v[posmin];
v[posmin] = tmp;
}
}
fine = clock();
//Visualizzazione vettore
for(i = 0; i < elem; i++){
printf("%d |", v[i]);
}
tempo = (double)(fine - inizio)/CLOCKS_PER_SEC;
printf(" Tempo: %f\n", tempo);
}
}
int main(){
srand(time(NULL));
int elementi = 20, i;
int v[elementi];
printf("Creazione vettore 20 elementi: \n");
for(i = 0; i < elementi; i++)
{
v[i] = rand() % 1001;
printf("%d |", v[i]);
}
ordinaVett(v, elementi);
}
What should I do for be able to see a normal output?
Thanks!
some words are in Italian so: tempo is time, ordinaVett is sortArray, inizio is start and fine is end.
Ok nevermind, I realized that a CPU is too fast and a double variable couldn't hold a so simple number. This's for sure a pretty dumb exercize to put on a school book.
So I'm creating a sudoku solver in C. Here's my full code as of now, I've mostly been using python and just got into C, I basically converted a lot of python functions to C to get this but I think it'll work:
#include <stdio.h>
#include <stdlib.h>
int is_empty();
int possible_v();
int solver();
int main(){
int s_array[9][9];
FILE * fpointer;
int i;
int j;
fpointer = fopen("sudoku001.txt", "r");
for (i=0; i<9; i++){
for(j = 0; j<9; j++){
fscanf(fpointer, "%d", &s_array[i][j]);
}
}
for (i=0; i<9; i++) {
if (i % 3 == 0) {
printf("------------------------------\n");
}
for (j = 0; j < 9; j++) {
printf(" %d ", s_array[i][j]);
if ((j + 1) % 3 == 0) {
printf("|");
}
}
printf("\n");
}
solver(s_array);
for (i=0; i<9; i++) {
if (i % 3 == 0) {
printf("------------------------------\n");
}
for (j = 0; j < 9; j++) {
printf(" %d ", s_array[i][j]);
if ((j + 1) % 3 == 0) {
printf("|");
}
}
printf("\n");
}
return 0;
}
int is_empty(int board[9][9]){
int i;
int j;
int is_empty= 0;
for (i=0; i<9; i++){
for(j = 0; j<9; j++){
if (board[i][j] == 0) {
is_empty = 1;
break;
}
}
if (is_empty == 1){
break;
}
}
return is_empty;
}
int possible_v(int board[9][9], int i, int j) {
int p_array[9] = {0, 0, 0, 0, 0, 0, 0, 0, 0};
int x;
int y;
int temp;
for (x = 0; x < 9; x++) {
if (board[x][j] != 0) {
temp = board[x][j];
p_array[temp - 1] = temp;
}
}
for (y = 0; y < 9; y++) {
if (board[i][y] != 0) {
temp = board[i][y];
p_array[temp - 1] = temp;
}
}
int m;
int n;
int temp1;
int temp2;
if (i>= 0 && i <= 2) {
m = 0;
}
else if (i>= 3 && i<=5) {
m = 3;
}
else{
m = 6;
}
if (j>= 0 && j <= 2) {
n = 0;
}
else if (j>= 3 && j<=5) {
n = 3;
}
else{
n = 6;
}
temp1 = m;
temp2 = n;
for (temp1; temp1<temp1+3; temp1++){
for (temp2; temp2<temp2+3; temp2++){
if (board[temp1][temp2] != 0){
p_array[board[temp1][temp2]] = 1;
}
}
}
temp1 = 1;
for (temp1; temp1<10){
if (p_array[temp1] == 0){
p_array[temp1] = temp1;
}
else{
p_array[temp1] = 0;
}
}
return p_array;
}
int solver(int board[9][9]){
int i;
int j;
int x;
int y;
int empty_check;
int p_values;
int temp;
if (is_empty(board) == 0){
printf("Board Completed");
empty_check = 0;
return empty_check;
}
else{
for (x = 0; x < 9; x++){
for (y = 0; y< 9; y++){
if (board[x][y] == 0){
i = x;
j = y;
break;
}
}
}
p_values = possible_v(board, i, j);
for (temp = 1; temp <10; temp++){
if (p_values[temp] != 0){
board[i][j] = p_values[temp];
solver(board);
}
}
board[i][j] = 0;
}
}
My main issue when compiling is getting the last two functions work with each other.
Function 'solver' calls and binds function 'possible_v'. Possible_V returns an array which I need to solve the puzzle. How can I make this work? .
You have the array locally declared, hence it cannot be passed back since it is destroyed once the function is exited. The workaround to this is to dynamically declare the array using malloc int *parray = (int*)malloc(9*sizeof(int)); and using the return type int* instead of int. But do not forget to free the allocated memory, else you will just keep allocating new memory from heap for every call you make.
As a side note, your implementation of Sudoku solver is a bit complex, and there is no need to return an array. You need to pass only the board. Here is an implementation of Sudoku Solver. This works both for 9x9 and 6X6 boards.
Edit : As advised by David Rankin, I have converted the C++ code to C.
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
int n;
int issafe(int **board,int i,int j,int num){
for(int k=0;k<n;k++)
if(board[i][k] == num || board[k][j] == num)
return 0;
int cellx,celly;
if(n==6){
cellx = (i/2)*2;
celly = (j/3)*3;
for(int l=cellx;l<cellx+2;l++)
for(int m=celly;m<celly+3;m++)
if(board[l][m] == num){
return 0;
}
return 1;
}
int root = sqrt(n);
cellx = (i/(root))*root;
celly = (j/(root))*root;
for(int l=cellx;l<cellx+root;l++)
for(int m=celly;m<celly+root;m++)
if(board[l][m] == num)
return 0;
return 1;
}
int solve(int **board,int i,int j){
if(i == n)
return 1;
if(j == n){
return solve(board,i+1,0);
}
if(board[i][j] != 0)
return solve(board,i,j+1);
for(int k=1;k<n+1;k++)
if(issafe(board,i,j,k)){
board[i][j] = k;
if(solve(board,i,j+1))
return 1;
//backtrack
board[i][j] = 0;
}
return 0;
}
int main(){
do{
printf("Enter size of board(9 or 6): ");
scanf("%d",&n);
}while(n != 9 && n != 6);
int **board;
board = malloc(sizeof *board * n);
for(int i=0;i<n;i++)
board[i] = malloc(sizeof *board * n);
// input
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
scanf("%d",&board[i][j]);
if(solve(board,0,0))
for(int i=0;i<n;i++){
for(int j=0;j<n;j++)
printf("%d ",board[i][j]);
printf("\n");
}
return 0;
}
This program is designed to essentially take user input of grades and output various functions listed. The main problem I have with this is that my pointer (processGrades) to functions will not work. It outputs the error "Called object type 'void' is not a function or function pointer". Is this not the correct implementation of pointers? What might be causing this?
#include <stdio.h>
void minimum(int students, int exams, int studentGrades[students][exams])
{
int lowGrade = 100;
size_t i, j;
for (i = 0; i < students; ++i) {
for (j = 0; j < exams; ++j) {
if (studentGrades[i][j] < lowGrade) {
lowGrade = studentGrades[i][j];
}
}
}
printf("Minimum grade : %d\n", lowGrade); // return minimum grade
return;
}
void maximum(int students, int exams, int studentGrades[students][exams])
{
int highGrade = 0; // initialize to lowest possible grade
size_t i, j;
for (i = 0; i < students; ++i) {
for (j = 0; j < exams; ++j) {
if (studentGrades[i][j] > highGrade) {
highGrade = studentGrades[i][j];
}
}
}
printf("Maximum grade : %d\n", highGrade); // return maximum grade
return;
}
void average(int students, int exams, int studentGrades[students][exams])
{
double total = 0.0; // sum of test grades
size_t i, j;
for (i = 0; i < students; ++i) {
for (j = 0; j < exams; ++j) {
total += studentGrades[i][j];
}
printf ("Average of student %lu : %f\n", i, total / exams); // average
total = 0;
}
return;
}
void printArray(int students, int exams, int studentGrades[students][exams])
{
size_t i, j;
printf("%s", " [0] [1] [2] [3]");
for (i = 0; i < students; ++i) {
printf("\nstudentGrades[%lu] ", i);
for (j = 0; j < exams; ++j) {
printf("%-5d", studentGrades[i][j]);
}
}
printf("\n");
return;
}
int main(void)
{
int choice;
int students;
int exams;
printf("Let's create a 2Dim array!\n\n");
printf("How many students? ");
scanf("%d", &students);
printf("\nHow many exams? ");
scanf("%d", &exams);
printf("\n");
int studentGrades[students][exams];
int i;
int j;
int entry;
for (i = 0; i < students; i++){
for (j=0; j < exams; j++) {
printf("enter [%d] [%d]: ", i,j);
scanf("%d",&entry);
studentGrades[i][j] = entry;
}
}
void (*processGrades[4]);
processGrades[0] = printArray;
processGrades[1] = minimum;
processGrades[2] = maximum;
processGrades[3] = average;
int stop = 0;
while(stop != 1) {
printf("Enter a choice:\n");
printf("0 Print the array of grades\n");
printf("1 Find the minimum grade\n");
printf("2 Find the maximum grade\n");
printf("3 Print the average on all tests for each student\n");
printf("4 End Program\n");
scanf("%d", &choice);
if(choice == 4){
stop = 1;
}
else{
(*processGrades[choice])(students)(exams)(studentGrades));
}
}
}
You can typedef a function pointer of the type of your process functions:
typedef void (*processGradesFnPtr)(int, int, int [*][*]);
And in the main, you can an declare an array of four function pointers and assign them accordingly.
processGradesFnPtr processGrades[4];
processGrades[0] = printArray;
processGrades[1] = minimum;
processGrades[2] = maximum;
processGrades[3] = average;
And when you call them, you have to call them like this:
(*processGrades[choice])(students, exams, (studentGrades));