This is my code
with the help of pointers i try to find and compare the sum of the main and second diagonal of a 5x5 matrix.The numbers are randomly generated
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main {
int i, s1, s2, j, a[5][5];
srand (time (NULL));
printf ("The matrix is:\n");
for (i = 0; i < 5; i++) {
printf ("\n\n");
for (j = 0; j < 5; j++) {
*(*(a + i) + j) = rand ();
printf ("%d ", *(*(a + i) + j));
}
}
for (i = 0; i < 5; i++) {
s1 += a[i][i]; // main diagonal
s2 += a[i][4 - i]; // second diagonal
}
printf ("\n\nThe sum 1:%d\nThe sum 2:%d", s1, s2);
if (s1 == s2) {
printf ("They are the same");
}
return 0;
}
The problem is that the sum of the main diagonal is bigger by 51 than it should be
s1 and s2 both are uninitialised. You need to initialize them with 0 before using in statements s1+=a[i][i]; and s2+=a[i][4-i];.
int i, s1 = 0, s2 = 0, j, a[5][5];
I would also suggest to use
a[i][j] = rand();
instead of
*(*(a+i)+j)=rand();
to access array members.
You have not initialised s1,s2 to zero
s1+=a[i][i]; // main diagonal
s2+=a[i][4-i]; // second diagonal
Prior to these statements
You could do as follow:
*(*(a+i)+j)=rand(); ----> a[i][j]=rand()%100;
printf("%d ",*(*(a+i)+j)); -----> printf("%d ", a[i][j]);
add s1 = s2 = 0;
Then the following could work.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main()
{
int i,s1,s2,j,a[5][5];
srand(time(NULL));
printf("The matrix is:\n");
for(i=0;i<5;i++)
{
printf("\n\n");
for(j=0;j<5;j++)
{
a[i][j]=rand()%100;
printf("%d ", a[i][j]);
}
}
s1 = s2 = 0;
for(i=0;i<5;i++)
{
s1+=a[i][i]; // main diagonal
s2+=a[i][4-i]; // second diagonal
}
printf("\n\nThe sum 1:%d\nThe sum 2:%d\n",s1,s2);
if (s1==s2)
{
printf("They are the same\n");
}
return 0;
}
For starters according to the C Standard function main without parameters shall be declared like
int main( void )
Secondly it is better to use a named constant instead of magic numbers. So you could define for example named constant N for the magic number 5. In this case your program will be more flexible and can be modified easy.
For the sums it is better to use a more larger integer type as long long int. Otherwise an overflow can occur.
You should initialize variables near the point in the program where the variables are used. If your compiler supports C99 then you may declare and initialize variables where they are used.
You could limit the maximum value that elements of the matrix can have.
The program can look the following way.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define N 5
#define VALUE_MAX 100
int main(void)
{
int a[N][N];
size_t i, j;
long long int s1, s2;
srand( ( unsigned int )time( NULL ) );
printf( "The matrix is:\n" );
for ( i = 0; i < N; i++ )
{
printf ( "\n\n" );
for ( j = 0; j < N; j++ )
{
*( *( a + i ) + j ) = rand() % VALUE_MAX;
printf ( "%d ", *( *( a + i ) + j ) );
}
}
s1 = 0ll;
s2 = 0ll;
for ( i = 0; i < N; i++ )
{
s1 += a[i][i]; // main diagonal
s2 += a[i][N - i - 1]; // second diagonal
}
printf( "\n\nThe sum 1: %lld\nThe sum 2: %lld\n", s1, s2 );
if ( s1 == s2 )
{
puts( "They are the same" );
}
return 0;
}
Its output might look like
The matrix is:
55 81 79 14 75
89 70 41 27 73
94 14 79 85 45
66 92 27 90 67
14 85 79 53 83
The sum 1: 377
The sum 2: 287
Or if your compiler supports C99 then the program can look like
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define N 5
#define VALUE_MAX 100
int main(void)
{
int a[N][N];
srand( ( unsigned int )time( NULL ) );
printf( "The matrix is:\n" );
for ( size_t i = 0; i < N; i++ )
{
printf ( "\n\n" );
for ( size_t j = 0; j < N; j++ )
{
*( *( a + i ) + j ) = rand() % VALUE_MAX;
printf ( "%d ", *( *( a + i ) + j ) );
}
}
long long int s1 = 0ll;
long long int s2 = 0ll;
for ( size_t i = 0; i < N; i++ )
{
s1 += a[i][i]; // main diagonal
s2 += a[i][N - i - 1]; // second diagonal
}
printf( "\n\nThe sum 1: %lld\nThe sum 2: %lld\n", s1, s2 );
if ( s1 == s2 )
{
puts( "They are the same" );
}
return 0;
}
Related
When I create a vector , let's say size of 5 , elements are 1,2,3,4,5 and I want to add at the location( for example index 2) the numbers 200 and 300 , the vector should look like 1 ,2 ,200,300,3,4,5.
#include <stdio.h>
#include <stdlib.h>
int main(){
int v[] ={1,2,3,4,5,6,7};
int n = sizeof(v)/sizeof(int);
int location,element;
printf("Enter location:");
scanf("%d",&location);
printf("Enter element:");
scanf("%d",&element);
for(int i = n - 1; i >= location;i--){
v[i + 1] = v[i];
}
v[location] = element;
for(int i = 0; i <= n;i++){
printf("%d ",v[i]);
}
}
You need to loop like this
for(;;){
int location,element;
printf("Enter location:");
scanf("%d",&location);
if(location == -1)
break;
printf("Enter element:");
scanf("%d",&element);
for(int i = n - 1; i >= location;i--){
v[i + 1] = v[i];
}
v[location] = element;
}
BUT you code is severely broken, you are adding to a vector thats of fixed size. you cannot do that
In this code
for (int i = n - 1; i >= location; i--) {
v[i + 1] = v[i];
}
n is the count of number of elements, so on the first loop you do
v[n] = v[n-1]
ie
v[7] = v[6]
well v[7] is off the end of the array (array indexes are 0 to 6) Thats very bad
You declared a fixed size array
int v[] ={1,2,3,4,5,6,7};
You can not enlarge it.
Thus this loop
for(int i = 0; i <= n;i++){
printf("%d ",v[i]);
}
invokes undefined behavior due to using the index with the value n that is outside the valid range of indices [0, n) for the source array.
You need to allocate the array dynamically and when you are going to add one more element you will need to reallocate the array.
Here is a demonstration program.
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
int main( void )
{
size_t n = 7;
int *v = malloc( n * sizeof( int ) );
memcpy( v, ( int [7] ){ 1, 2, 3, 4, 5, 6, 7 }, n * sizeof( int ) );
for ( size_t i = 0; i < n; i++ )
{
printf( "%d ", v[i] );
}
putchar( '\n' );
size_t location = 0;
printf( "Enter location: " );
scanf( "%zu", &location );
if ( n < location ) location = n;
int element = 0;
printf( "Enter element: " );
scanf( "%d",&element );
int *tmp = realloc( v, ( n + 1 ) * sizeof( int ) );
if ( tmp != NULL )
{
v = tmp;
memmove( v + location + 1, v + location, ( n - location ) * sizeof( int ) );
v[location] = element;
++n;
}
for ( size_t i = 0; i < n; i++ )
{
printf( "%d ", v[i] );
}
putchar( '\n' );
free( v );
}
The program output might look like
1 2 3 4 5 6 7
Enter location: 2
Enter element: 200
1 2 200 3 4 5 6 7
#ifdef _MSC_VER
#define _CRT_SECURE_NO_WARNING
#endif
#include <stdio.h>
#include <stdlib.h>
void bubbleSort(int matrix[9], int n);
int main(void) {
int matrix[9];
int n = 9;
printf("enter 10 values to the matirx \n");
for (int i = 0; i < 10; i++) {
printf("now %dth componet\n ", i+1);
scanf("%d", &matrix[i]);
}
bubbleSort(matrix[9], n);
return 0;
}
void bubbleSort(int matrix [9], int n) {
//bubble sort the given matrix
int temp = 0;
for (int i=n-1; i > 0; i--) {
// compare two values at j and j+1 and move left when j+1 is smaller than j
for (int j = 0; j < i; j++) {
if (matrix[j] > matrix[j + 1]) {
temp = matrix[j];
matrix[j] = matrix[j + 1];
matrix[j + 1] = temp;
}
}
printf("Check the matrix \n");
for (int i = 0; i < 9; i++) {
printf("%d ", matrix[i]);
}
printf("\n");
}
}
Hi, I am getting a read access violation error at
if (matrix[j] > matrix[j + 1]) {
**temp = matrix[j];**
matrix[j] = matrix[j + 1];
matrix[j + 1] = temp;
}
This part of the code. The code builds right, but when I run the program I get an error. Can anyone help me out to figure out the issue? I searched up a little bit and based on that I assume it has something to do with the pointer but I do not know why there would be an issue with a pointer since I never used it in my code.
For starters you declared an array that contains only 9 elements.
int matrix[9];
On the other hand, you are trying to enter 10 elements.
printf("enter 10 values to the matirx \n");
for (int i = 0; i < 10; i++) {
printf("now %dth componet\n ", i+1);
scanf("%d", &matrix[i]);
}
So the program already has undefined behavior.
In this function declaration
void bubbleSort(int matrix[9], int n);
the magic number 9 used in the declaration of the first parameter does not make a great sense. Just write
void bubbleSort(int matrix[], int n);
or
void bubbleSort(int *matrix, int n);
In this call
bubbleSort(matrix[9], n);
instead of passing the array matrix like
bubbleSort(matrix, n);
you are passing the non-existent element of the array matrix[9]. The compiler should issue a message that you are trying to convert an integer to a pointer.
Within the function you are using the magic number 9 instead of the parameter n in this loop
for (int i = 0; i < 9; i++) {
Pay attention to that it looks strange when a one-dimensional array is named matrix.
Using your approach the program can look the following way.
#include <stdio.h>
void bubbleSort( int matrix[], size_t n )
{
if ( n )
{
for ( size_t i = n - 1; i != 0; i-- )
{
// compare two values at j and j+1 and move left when j+1 is smaller than j
for ( size_t j = 0; j < i; j++ )
{
if ( matrix[j + 1] < matrix[j] )
{
int temp = matrix[j];
matrix[j] = matrix[j + 1];
matrix[j + 1] = temp;
}
}
}
}
}
int main(void)
{
enum { N = 9 };
int matrix[N];
printf( "enter %d values to the matirx \n", N );
for ( int i = 0; i < N; i++ )
{
printf( "now %dth componet\n", i+1 );
scanf( "%d", matrix + i );
}
bubbleSort( matrix, N );
for ( int i = 0; i < N; i++ )
{
printf( "%d", matrix[i] );
}
putchar( '\n' );
return 0;
}
The program output might look
enter 9 values to the matirx
now 1th componet
9
now 2th componet
8
now 3th componet
7
now 4th componet
6
now 5th componet
5
now 6th componet
4
now 7th componet
3
now 8th componet
2
now 9th componet
1
1 2 3 4 5 6 7 8 9
bubbleSort(matrix[9], n);
This passes just the final element of the list (coerced into an address) rather than the actual address of the list, which is what you probably intended.
That won't end well :-)
You should just pass in matrix.
A decent compiler should warn you of this, such as with gcc:
prog.c: In function ‘main’:
prog.c:21:22: warning: passing argument 1 of ‘bubbleSort’
makes pointer from integer without a cast
[-Wint-conversion]
21 | bubbleSort(matrix[9], n);
| ~~~~~~^~~
| |
| int
prog.c:7:21: note: expected ‘int *’ but argument is of
type ‘int’
7 | void bubbleSort(int matrix[9], int n);
| ~~~~^~~~~~~~~
May you help me with this exercise please?
Write a C program that reads 6 integers from the keyboard and assigns the first 5 values at the first 5 positions of an array; store the sixth value in a variable N. Write a function that, given input the array initialized with the first 5 values from the keyboard and the integer N, returns the array resized to contain 5 + N elements, such that each one of the new N elements corresponds to the sum of the numbers before it in the array.
In main, print the content of the array returned by the function.
It's OK also all in the main function.
I have the problem when I have to use the function realloc to increment the array from size = 5 to 5 + N.
This is my code:
int N, a, i;
int *ptr;
int arr[6];
for (i = 0; i < 5; i++) {
printf("Insert number in array, position(%d): ", i);
scanf("%d", &arr[i]);
}
N = arr[4];
a = 5 + N;
ptr = (int *)realloc(arr, sizeof(int) * a);
for (i = 4; i < a; i++) {
ptr + i = N * N; //<--- **problem!!**
}
for (i = 0; i < a; i++) {
printf("%d\n", ptr[i]);
}
free(ptr);
You cannot reallocate an array defined locally in a function nor defined globally. You can only call realloc on an object previously allocated with malloc(), calloc() or realloc() or with a NULL pointer. So you must allocate the initial array with 5 elements in main() and reallocate it in the function.
#include <stdio.h>
#include <stdlib.h>
int *extend_array(int *arr, int N) {
int a = 5 + N;
arr = realloc(arr, a * sizeof(int));
if (arr != NULL) {
int sum = 0;
for (int i = 0; i < 5; i++) {
sum += arr[i];
}
for (int i = 5; i < a; i++) {
arr[i] = sum;
sum += sum;
}
}
return arr;
}
int main() {
int N;
int *arr = malloc(5 * sizeof(int));
if (arr == NULL) {
printf("allocation failed\n");
return 1;
}
for (int i = 0; i < 5; i++) {
printf("Insert number in array, position(%d): ", i);
if (scanf("%d", &arr[i]) != 1) {
printf("invalid input\n");
return 1;
}
}
printf("Insert the value of N: ");
if (scanf("%d", &N) != 1) {
printf("invalid input\n");
return 1;
}
int *ptr = extend_array(arr, N);
if (ptr == NULL) {
printf("reallocation failed\n");
} else {
arr = ptr;
for (int i = 0; i < 5 + N; i++) {
printf("%d\n", arr[i]);
}
}
free(arr);
return 0;
}
The assignment specifies that the function should take the array and the number N as arguments, but it would be better to make the initial size a variable and pass that to the function as well to make the code more generic, easier to extend, and less error prone as the constant 5 appears in many places.
If you need to reallocate an array then it initially must be allocated dynamically.
So this code
int arr[6];
//...
ptr = (int *)realloc(arr, sizeof(int) * a);
is invalid.
Pay attention to that according to the assignment you need to write a function that reallocates the array.
Bear in mind that it is a very bad style of programming to use "magic numbers" like 5. Instead use named constants or assign such numbers to variables and use them.
The program can look the following way.
#include <stdio.h>
#include <stdlib.h>
int * resize( int *a, size_t n, size_t m )
{
int *tmp = realloc( a, ( n + m ) * sizeof( int ) );
if ( tmp != NULL )
{
int sum = 0;
size_t i = 0;
while ( i < n ) sum += tmp[i++];
while ( i < n + m )
{
tmp[i] = sum;
sum += tmp[i++];
}
}
return tmp;
}
int main(void)
{
size_t n = 5;
int *a = malloc( n * sizeof( int ) );
size_t m = 0;
printf( "Enter %zu numbers. The last number shall be greater than 0: ", n + 1 );
for ( size_t i = 0; i < n; i++ )
{
scanf( "%d", a + i );
}
scanf( "%zu", &m );
int *tmp = resize( a, n, m );
if ( tmp != NULL )
{
a = tmp;
}
else
{
m = 0;
}
for ( size_t i = 0; i < n + m; i++ )
{
printf( "%d ", a[i] );
}
putchar( '\n' );
free( a );
return 0;
}
The program output might look like
Enter 6 numbers. The last number shall be greater than 0: 1 2 3 4 5 6
1 2 3 4 5 15 30 60 120 240 480 960 1920 3840 7680
Given two arrays
a = [1,3,2,8,5,6]
b = [4,3,2,5,1,2]
I would like to sort them in "one pass" where the "logic" involves the two arrays, meaning:
if
int sortBySum(index i, index j){
if (a[i]+b[i] > a[j]+b[j]){
return 1;
}
return -1;
}
The output of the two arrays will be
a = [2,1,3,5,6,8]
b = [2,4,3,1,2,5]
Because the original arrays sum is a+b=[5,6,4,13,6,8]
When a task is about sorting, qsort is - in most cases - your friend.
But qsort can only sort one array. So you need to copy the elements from array a and b into an array of structs containing the data from a and b. Then use qsort and then copy the data back.
Like:
#include <stdio.h>
#include <stdlib.h>
typedef struct
{
int a;
int b;
} common_data;
// Compare function for qsort
int cmp(const void *p1, const void *p2)
{
common_data* pa = (common_data*)p1;
common_data* pb = (common_data*)p2;
return ((pa->a + pa->b) - (pb->a + pb->b));
}
int main(void)
{
int a[] = {1,3,2,8,5,6};
int b[] = {4,3,2,5,1,2};
size_t sz = sizeof(a)/sizeof(a[0]);
common_data carr[sz];
// Copy to common array
for (size_t i = 0; i < sz; ++i)
{
carr[i].a = a[i];
carr[i].b = b[i];
}
// Sort common array
qsort(carr, sz, sizeof(carr[0]), cmp);
// Copy to back to original array
for (size_t i = 0; i < sz; ++i)
{
a[i] = carr[i].a;
b[i] = carr[i].b;
}
// Print array a
for (size_t i = 0; i < sz; ++i)
{
printf("%d ", a[i]);
}
printf("\n");
// Print array b
for (size_t i = 0; i < sz; ++i)
{
printf("%d ", b[i]);
}
printf("\n");
return 0;
}
Output:
2 1 3 5 6 8
2 4 3 1 2 5
A primitive approach can look the following way.
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int a[] = { 1, 3, 2, 8, 5, 6 };
int b[] = { 4, 3, 2, 5, 1, 2 };
const size_t N = sizeof( a ) / sizeof( *a );
printf( "a: " );
for ( size_t i = 0; i < N; i++ )
{
printf( "%d ", a[i] );
}
putchar( '\n' );
printf( "b: " );
for ( size_t i = 0; i < N; i++ )
{
printf( "%d ", b[i] );
}
putchar( '\n' );
putchar( '\n' );
for ( size_t n = N, last; !( n < 2 ); n = last )
{
for ( size_t i = last = 1; i < n; i++ )
{
if ( a[i] + b[i] < a[i-1] + b[i-1] )
{
int tmp = a[i];
a[i] = a[i-1];
a[i-1] = tmp;
tmp = b[i];
b[i] = b[i-1];
b[i-1] = tmp;
last = i;
}
}
}
printf( "a: " );
for ( size_t i = 0; i < N; i++ )
{
printf( "%d ", a[i] );
}
putchar( '\n' );
printf( "b: " );
for ( size_t i = 0; i < N; i++ )
{
printf( "%d ", b[i] );
}
putchar( '\n' );
putchar( '\n' );
return 0;
}
The program output is
a: 1 3 2 8 5 6
b: 4 3 2 5 1 2
a: 2 1 3 5 6 8
b: 2 4 3 1 2 5
The code that sorts the arrays using the method bubble sort can be moved in a separate function.
Given the set of unsorted integers, how to find every pair of integers which have minimum difference. There are 3 samples as described below:
a = random.sample (range(-200,200), 5)
b = random.sample (range(-1000, 1000), 25)
c = random.sample (range(-2000, 2000), 50)
The expected output should be something like:
List A = [-85, -154, -33, 192, -160]
Minimum pairs for list A:
(-160, -154)
Catch! :)
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
struct Pair
{
size_t first;
size_t second;
};
struct Pair minimum_difference( const int a[], size_t n )
{
struct Pair p = { 0 };
if ( 1 < n )
{
p.first = 0;
p.second = 1;
for ( size_t i = 0; i < n - 1; i++ )
{
for ( size_t j = i + 1; j < n; j++ )
{
// printf( "%d %d %llu\n", a[i], a[j],
// ( unsigned long long )abs( a[i] - a[j] ) );
if ( ( unsigned long long )abs( a[i] - a[j] ) <
( unsigned long long )abs( a[p.first] - a[p.second] ) )
{
p.first = i;
p.second = j;
}
}
}
}
return p;
}
int main(void)
{
const size_t N = 5;
const int UPPER_BOUND = 40 * N;
int a[N];
srand( ( unsigned int )time( NULL ) );
for ( size_t i = 0; i < N; i++ )
{
a[i] = rand() % ( 2 * UPPER_BOUND ) - UPPER_BOUND;
}
for ( size_t i = 0; i < N; i++ ) printf( "%d ", a[i] );
printf( "\n" );
struct Pair p = minimum_difference( a, N );
printf( "(%d, %d)\n", a[p.first], a[p.second] );
return 0;
}
The program output might look loke
119 9 -193 21 -43
(9, 21)
This prpgram finds only the first pair with the minimum difference. If there are several pairs with the minimum difference when you have to allocate dynamically an array of pairs. Of course the function will be changed.
The other approach is to use a sort-copy algorithm by using an additional array. And then traverse this array calculating the difference.