array placement when user input another data - c

user input 10 data, after that, user input 1 number. and the program will print out the placement of where the number store in the array.
int main() {
int arr[10];
int data,num;
for (int i = 0; i < 10; i++) {
printf("%d ",i );
scanf("%d",&arr[i]);
}
printf("Input data : " );
scanf("%d",&data );
for (int j = 0; j < arr[data]; j++) {
if (data==arr[j]) {
printf("Data %d found in array %d\n",data,arr[j] );
break;
}
else {
printf("Data not found in array\n");
break;
}
}
return 0;
}

int main() {
int arr[10];
int data, flag = 0;
for (int i = 0; i < 10; i++) {
printf("%d ",i );
scanf("%d",arr[i]);
}
printf("Input data : " );
scanf("%d",&data );
for (int j = 0; j < 10; j++) {
if (data==arr[j]) {
printf("Data %d found in array at %dth position\n",data,j);
flag = 1;
break;
}
}
if(!flag){
printf("data not found in the array\n");
}
return 0;
}

Related

Havel-Hakimi Algorithm in C

I'm trying to write havel-hakimi theorem in C. But I have a problem with the while loop. The program doesn't sort array again in the while loop and that's why output prints the wrong answer. Could show me what's my fault please?
# include <stdio.h>
int main(){
int j,i,vertex_number,temp1,temp2,a=0,b=0;
printf("Vertex Number:");
scanf("%d",&vertex_number);
int graph[vertex_number];
for(i=0;i<vertex_number;i++){
scanf("%d",&graph[i]);
}
while(1){
//SORTING ARRAY
for(i=0;i<vertex_number;i++){
for(j=i+1;j<vertex_number;j++){
if(graph[i]<graph[j]){
temp1=graph[i];
graph[i]=graph[j];
graph[j]=temp1;
}
}
}
//IF ALL VERTEX DEGREES EQUAL 0 GRAPH EXIST
for(i=0;i<vertex_number;i++){
if(graph[i]==0){
a++;
}
}
if(a==vertex_number){
printf(" graph exist.");
return 0;
}
//NEGATIVE VERTEX DEGREE NOT EXIST
for(i=0;i<vertex_number;i++){
if(graph[i]<0){
b++;
}
}
if(b>0){
printf("graph not exist.");
return 0;
}
temp2=graph[0];
for(i=0;i<temp2;i++){
graph[i]=graph[i+1];
}
vertex_number--;
for(i=0;i<temp2;i++){
graph[i]-=1;
}
printf("-------------\n");
for(i=0;i<vertex_number;i++){
printf("%d\n",graph[i]);
}
}
}
Your have 2 issues, the exist if graph[i]-=1; is negative and the removing loop doing should be done for vertex_number and not temp2
# include <stdio.h>
int main(void) {
int i, j, vertex_number, temp1, temp2;
printf("Vertex Number:");
scanf("%d", &vertex_number);
int graph[vertex_number];
for (i = 0; i < vertex_number; i++){
scanf("%d", &graph[i]);
}
while (1) {
//SORTING ARRAY
for (i = 0; i < vertex_number; i++) {
for (j = i+1; j < vertex_number; j++) {
if (graph[i] < graph[j]) {
temp1 = graph[i];
graph[i] = graph[j];
graph[j] = temp1;
}
}
}
//IF ALL VERTEX DEGREES EQUAL 0 GRAPH EXIST
if (graph[0] == 0) {
printf(" graph exist.");
return 0;
}
//NEGATIVE VERTEX DEGREE NOT EXIST
for (i = 0; i < vertex_number; i++) {
if (graph[i] < 0){
printf("graph not exist.");
return 0;
}
}
temp2 = graph[0];
vertex_number--;
for (i = 0; i < vertex_number; i++) { // HERE was your issue
graph[i] = graph[i + 1];
}
for (i = 0; i < temp2; i++) {
graph[i]-=1;
if (graph[i] < 0) {
printf("graph not exist.");
return 0;
}
}
printf("-------------\n");
for (i = 0; i < vertex_number; i++) {
printf("%d\n",graph[i]);
}
}
}
You don't need a, if the first is null all the other are null or negative
You don't need b neither just stop on first occurence
Not an answer, but a cleaned-up version that works follows.
The key is that it literally removes the first element from the array by advancing the pointer and reducing the size of the array. That way, we're always working with elements 0..s-1 or 0..n-1.
// Destroys the contents of the provided array.
int havel_hakimi(unsigned *degrees, size_t n) {
while (1) {
// Yuck
for (size_t i=0; i<n; ++i) {
for (size_t j=i+1; j<n; ++j) {
if (degrees[i] < degrees[j]) {
int temp = degrees[i];
degrees[i] = degrees[j];
degrees[j] = temp;
}
}
}
if (degrees[0] == 0)
return 1; // Has a simple graph.
// Remove first element.
unsigned s = degrees[0];
++degrees;
--n;
if (s > n)
return 0; // Invalid input!
if (degrees[s-1] == 0)
return 0; // Doesn't have a simple graph.
for (size_t i=s; i--; )
--degrees[i];
}
}
Tested using the following:
#include <stdio.h>
int main(void) {
{
unsigned degrees[] = { 6, 3, 3, 3, 3, 2, 2, 2, 2, 1, 1 };
printf("%d\n", havel_hakimi(degrees, sizeof(degrees)/sizeof(degrees[0]))); // 1
}
{
unsigned degrees[] = { 6, 5, 5, 4, 3, 2, 1 };
printf("%d\n", havel_hakimi(degrees, sizeof(degrees)/sizeof(degrees[0]))); // 0
}
return 0;
}

Sort a matrix row wise (c programming)

I wrote this code for sorting an nxn matrix: odd rows in descending order, double rows in ascending order, but it doesn't pass the compiler stage.
What did I do wrong?
It mostly tells me this: assignment to 'int' from 'int *' makes integer from pointer without a cast (how can I solve this problem)?
#include <stdio.h>
#include <stdlib.h>
int comp_a(int a, int b) {
if (a < b) {
return 1;
} else {
return 0;
}
}
int comp_d(int a, int b) {
if (a > b) {
return 1;
} else {
return 0;
}
}
void sort(int a[], int n, int (*comp)(int, int)) {
int t;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n - 1; j++) {
if (comp(a[j], a[j + 1]) == 0) {
t = a[j];
a[j] = a[j + 1];
a[j + 1] = t;
}
}
}
}
int main() {
int n;
printf("Enter the dimension of matrix(n): ");
scanf("%d", &n);
int *mat = (int*)calloc(n, sizeof(int));
for (int i = 0; i < n; i++) {
mat[i] = (int*)calloc(n, sizeof(int));
}
for (int i = 0; i < n; i++) {
printf("Enter row [%d]: ", i);
for (int j = 0; j < n; j++) {
scanf("%d", &mat[i][j]);
}
}
for (int i = 0; i < n; i++) {
if (i%2 == 0) {
sort(mat[i], n, &comp_a);
} else {
sort(mat[i], n, &comp_d);
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
printf("%d ", mat[i][j]);
}
printf("\n");
}
return 0;
}
You should change your matrix allocation to this:
int **mat = (int**)calloc(n, sizeof(int*)); // (1)
for(int i = 0; i < n; i++) {
mat[i] = (int*)calloc(n, sizeof(int)); // (2)
}
(1): Declaring an array of pointers of size n x int*.
(2): Where each pointer in this array points to an array of integers of size n x int.
I changed your matrix allocation
int *mat = (int*)calloc(n, sizeof(int));
for(int i = 0; i < n; i++) {
mat[i] = (int*)calloc(n, sizeof(int));
}
to
int mat[n][n];
I changed for loop of ordred
for(int i = 0; i < n; i++) {
for(int j = 0; j < n - 1; j++) {
if(comp(a[j], a[j + 1]) == 0)
because he take a lot of time (time of running) to
for(int i = 0; i < n; i++) {
for(int j = i+1; j < n; j++) {
if(comp(a[i], a[j]) == 0)
my code:
#include <stdio.h>
#include <stdlib.h>
int comp_a(int a, int b) {
if(a < b) {
return 1;
}
else {
return 0;
}
}
int comp_d(int a, int b) {
if(a > b) {
return 1;
}
else {
return 0;
}
}
void sort(int a[], int n, int (*comp)(int, int)) {
int t;
for(int i = 0; i < n; i++) {
for(int j = i+1; j < n; j++) {
if(comp(a[i], a[j]) == 0) {
t = a[j];
a[j] = a[i];
a[i] = t;
}
}
}
}
int main() {
int n;
do
{
printf("Enter the dimension of matrix(n): ");
scanf("%d", &n);
}while(n<1);
int mat[n][n];
for(int i = 0; i < n; i++) {
printf("Enter row [%d]: ", i);
for(int j = 0; j < n; j++) {
scanf("%d", &mat[i][j]);
}
}
for(int i = 0; i < n; i++) {
if(i%2 == 0) {
sort(mat[i], n, &comp_a);
}
else {
sort(mat[i], n, &comp_d);
}
}
for(int i = 0; i < n; i++) {
for(int j = 0; j < n; j++) {
printf("%d ", mat[i][j]);
}
printf("\n");
}
return 0;
}

How to changing size of dynamically allocated matrix?

I have an C exercice at school. I had to make a dynamically allocated matrix filled with random number and which have a size choosing by user. Then, the program ask to the user to redefine a bigger size, and the new matrix have to keep old values and fill new values with random.
The program is blocking when I'm trying to redefine the size of the matrix. I'm receiving a segmentation fault as of line 73. I dont understand why :|
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<stddef.h>
int nbRand() {
static int first = 0;
if(first == 0) {
srand(time(NULL));
first = 1;
}
return rand()%10;
}
int main(int argc, char *argv[] ){
int **matrice, **matrice2; // Tableau
int i, j; // Compteur
int longueur1, largeur1, longueur2, largeur2; // Taille dynamique tableau
size_t n;
printf("Saisissez la taille du tableau.\n");
printf("Longueur : ");
scanf("%d", &longueur1);
printf("Largeur : ");
scanf("%d", &largeur1);
matrice = (int**) malloc(longueur1*sizeof(int*));
for(i = 0; i < longueur1; i++) {
matrice[i] = (int*) malloc(largeur1*sizeof(int));
}
/* Autre manière
matrice = (int*) malloc(longueur1*largeur1*sizeof(int));
*/
for(i = 0; i < longueur1; i++) {
for(j = 0; j < largeur1; j++) {
matrice[i][j] = nbRand();
}
}
for(i = 0; i < longueur1; i++) {
for(j = 0; j < largeur1; j++) {
printf("%d ", matrice[i][j]);
}
printf("\n");
}
// Agrandir la matrice
printf("NOUVELLE TAILLE\n");
do {
printf("Longueur : ");
scanf("%d", &longueur2);
} while(longueur2 < longueur1);
do {
printf("Largeur : ");
scanf("%d", &largeur2);
} while(largeur2 < largeur1);
matrice2 = (int**) realloc(matrice, longueur2*sizeof(int*));
for(i = 0; i < longueur2; i++) {
matrice2[i] = (int*) realloc(matrice[i], largeur2*sizeof(int));
}
matrice=matrice2;
for(i = 0; i < longueur2; i++) {
for(j = 0; j < largeur2; j++) {
matrice[i][j] = nbRand();
printf("%d\t%d\t%d\n", i, j, matrice[i][j]);
}
}
printf("test\n");
for(i = 0; i < longueur2; i++) {
for(j = 0; j < largeur2; j++) {
printf("%d ", matrice[i][j]);
}
printf("\n");
}
}
EDITED :
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<stddef.h>
int nbRand() {
static int first = 0;
if(first == 0) {
srand(time(NULL));
first = 1;
}
return rand()%10;
}
int main(int argc, char *argv[] ){
int** matrice;
int** tmp; // Tableau
int i, j; // Compteur
int longueur1, largeur1, longueur2, largeur2; // Taille dynamique tableau
size_t n;
printf("Saisissez la taille du tableau.\n");
printf("Longueur : ");
scanf("%d", &longueur1);
printf("Largeur : ");
scanf("%d", &largeur1);
matrice = (int**) malloc(longueur1*sizeof(int*));
for(i = 0; i < longueur1; i++) {
*(matrice+i) = (int*) malloc(largeur1*sizeof(int));
}
for(i = 0; i < longueur1; i++) {
for(j = 0; j < largeur1; j++) {
matrice[i][j] = nbRand();
}
}
for(i = 0; i < longueur1; i++) {
for(j = 0; j < largeur1; j++) {
printf("%d ", matrice[i][j]);
}
printf("\n");
}
// Agrandir la matrice
printf("NOUVELLE TAILLE\n");
do {
printf("Longueur : ");
scanf("%d", &longueur2);
} while(longueur2 < longueur1);
do {
printf("Largeur : ");
scanf("%d", &largeur2);
} while(largeur2 < largeur1);
tmp = (int**) realloc(matrice, longueur2*sizeof(int*));
for(i = 0; i < longueur2; i++) {
*(tmp + i) = (int*) realloc(*(tmp+i), largeur2*sizeof(int));
}
matrice=tmp;
for(i = longueur1; i < longueur2; i++) {
for(j = largeur2; j < largeur2; j++) {
matrice[i][j] = nbRand();
// printf("%d\t%d\t%d\n", i, j, matrice[i][j]);
}
}
printf("test\n");
for(i = 0; i < longueur2; i++) {
for(j = 0; j < largeur2; j++) {
printf("%d ", matrice[i][j]);
}
printf("\n");
}
}
matrice2 = (int**) realloc(matrice, longueur2*sizeof(int*));
for(i = 0; i < longueur2; i++) {
matrice2[i] = (int*) realloc(matrice[i], largeur2*sizeof(int));
^^^^^^^^^^
|
-> You are accessing old pointer here.
}
is wrong, it should be
matrice2 = (int**) realloc(matrice, longueur2*sizeof(int*));
for(i = 0; i < longueur2; i++) {
matrice2[i] = realloc(matrice2[i], largeur2*sizeof(int));
}

Printing patterns using arrays in c

The problem is to print this pattern:
5555555555
5444444445
5433333345
5432222345
5432112345
5432112345
5432222345
5433333345
5444444445
5555555555
This is my code:
#include<stdio.h>
int main()
{
int i,k,j,n,p;
printf("enter the no : ");
scanf("%d",&n);
p = n;
k = 0;
int a[2*n][2*n];
while (p>=1)
{
for(i=0+k;i<2*n-k;i++)
{
for(j=0+k;j<2*n-k;j++)
{
if(i == 2*n-k||i == k||j == k||j == 2*n-k)
{
a[i][j]=p;
}
else
{
a[i][j]= 8;
}
}
printf("\n");
}
k++,p--;
}
for(i=0;i<2*n;i++)
{
for(j=0;j<2*n;j++)
{
printf("%d",a[i][j]);
}
printf("\n");
}
return 0;
}
The result I get is:
5555555555
5444444448
5433333388
5432222888
5432118888
5432188888
5432888888
5438888888
5488888888
5888888888
Your code is overly complex. You should utilize the symmetry. Replace you while-loop with this code.
// Because of symmetry, both i and j can loop to n instead of 2*n
for(i=0; i<n; i++) {
p = n;
for(j=0; j<n; j++) {
// Assign four cells at once due to both horizontal and vertical symmetry
a[i][j] = a[i][2*n-j-1] = a[2*n-i-1][j] = a[2*n-i-1][2*n-j-1] = p;
if(j<i)
p--;
}
}

Array rotation in C

I am trying to rotate an array that looks like this:
a a a a
a b a a
b b b a
a a a a
I am supposed to rotate it 5 times for 90 degrees. It is supposed to be done in C.
I appreciate every help because I am just a beginner and am stuck on this.
Thanks in advance.
#include <stdio.h>
int main()
{
char array_1[4][4] = { {'-','-','-','-'},
{'-','o','-','-'},
{'o','o','o','-'},
{'-','-','-','-'}};
char array_2[4][4] = { {'-','-','-','-'},
{'-','o','o','-'},
{'o','o','-','-'},
{'-','-','-','-'}};
char array_3[4][4] = { {'-','-','-','-'},
{'-','o','-','-'},
{'-','o','-','-'},
{'-','o','o','-'}};
char array_4[4][4] = { {'-','-','o','-'},
{'-','-','o','-'},
{'-','-','o','-'},
{'-','-','o','-'}};
int counter = 0;
int counter_1 = 0;
for(counter = 0; counter < 4; counter++)
{
for(counter_1 = 0; counter_1 < 4; counter_1++)
{
printf("%c ",array_1[counter][counter_1]);
}
printf(" ");
for(counter_1 = 0; counter_1 < 4; counter_1++)
{
printf("%c ",array_2[counter][counter_1]);
}
printf(" ");
for(counter_1 = 0; counter_1 < 4; counter_1++)
{
printf("%c ",array_3[counter][counter_1]);
}
printf(" ");
for(counter_1 = 0; counter_1 < 4; counter_1++)
{
printf("%c ",array_4[counter][counter_1]);
}
printf(" ");
printf("\n");
}
printf("\n");
for(counter= 0; counter < 4; counter++)
{
for(counter_1 = 3; counter_1 >= 0; counter_1--)
{
printf("%c ",array_1[counter_1][counter]);
}
printf(" ");
for(counter_1 = 3; counter_1 >= 0; counter_1--)
{
printf("%c ",array_2[counter_1][counter]);
}
printf(" ");
for(counter_1 = 3; counter_1 >= 0; counter_1--)
{
printf("%c ",array_3[counter_1][counter]);
}
printf(" ");
for(counter_1 = 3; counter_1 >= 0; counter_1--)
{
printf("%c ",array_4[counter_1][counter]);
}
printf(" ");
printf("\n");
}
printf("\n");
like this:
#include <stdio.h>
typedef struct point { int x, y; } Point;
void rotate(int n, char array[n][n]){
//rotate right 90 degrees
if(n == 1) return ;
int times = n / 2;
for(int i = 0; i < times; ++i){
Point base = { i, i };
for(int j = 0; j < n - 1; ++j){
Point transition[4] = { {j, n-1}, {n-1,n-1-j},{n-1-j,0},{0,j} };
char curr = array[base.x][base.y+j];//base + {0,j}
for(int k = 0; k < 4; ++k){
char temp = array[base.x + transition[k].x][base.y + transition[k].y];
array[base.x + transition[k].x][base.y + transition[k].y] = curr;
curr = temp;
}
}
n -= 2;
}
}
void display(int n, char array[n][n]){
for(int i = 0; i < n; ++i){
for(int j = 0; j < n; ++j){
if(j)
putchar(' ');
putchar(array[i][j]);
}
putchar('\n');
}
putchar('\n');
}
int main(void){
//demo
char array4[4][4] = {
{'1','2','3','4'},
{'5','6','7','8'},
{'9','A','B','C'},
{'D','E','F','0'}
};
display(4, array4);
int n = 4;
while(n--){
rotate(4, array4);
display(4, array4);
}
char array5[5][5] = {
{'A','B','C','D','E'},
{'F','G','H','I','J'},
{'K','L','M','N','O'},
{'P','Q','R','S','T'},
{'U','V','W','X','Y'}
};
display(5, array5);
n = 4;
while(n--){
rotate(5, array5);
display(5, array5);
}
}

Resources