reversing elements of an integer array - arrays

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int reverse(int a[20],int n);
int main()
{
int a[20];
int n;
printf("enter the number of elements \n");
scanf("%d",&n);
printf("enter the array elements\n");
for(int i=0;i<n;i++)
{
scanf("%d ",&a[i]);
}
reverse(a,n);
return 0;
}
int reverse(int a[20],int n)
{
for(int i=n-1;i>=0;i--)
{
printf("%d ",a[i]);
}
return 0;
}
here if I input n=4 then during runtime i have to take 5 elements and then it reverses.For eg if i take n=4 and then for no of elements i have to take 1,2,3,4,5 and then only output is coming as 4 3 2 1.Why? is my logic wrong? also in this code I am unable to take the number of elements of arrays in a straight line, like 1 2 3 4.When I am entering the number each number is entering in new line .I am a novice programmer in C and thus having these doubts.Please anyone explain...

The problem with your code is the extra space after %d in your scanf line where you accept array elements i.e.
for(int i=0;i<n;i++)
{
scanf("%d ",&a[i]); //should be scanf("%d",&a[i]);
}
Change that and you're good to go.

Here is your entire program refactored to work correctly:
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int reverse(int a[20], int n)
int main()
{
int a[20];
int n;
printf("enter the number of elements \n");
scanf("%d",&n);
printf("enter the array elements\n");
for (int i=0; i<n; i++)
{
scanf("%d",&a[i]);
}
reverse(a,n);
return 0;
}
int reverse(int a[20], int n)
{
int mid = n/2;
for (int i=0; i < mid; ++i)
{
int temp = a[n-i-1];
a[n-i-1] = a[i];
a[i] = temp;
}
return 0;
}

Related

Program in c array

textI have to make one array which is A[n][n]. In this array the user puts some numbers and from this array I have to make a second array which is C[n] which numbers are positive number by row from first one. I try do it by this way but in that way it works for every numbers it have to be only by rows .have to look something like that for example
A00:1 A01:-3 A02:5
A10:-7 A11:-8 A12:7
A20:6 A21:9 A22:10
C00:2 C01:1 C02:3
But in my way it look :
C00:3 C01:3 C02:3 which is wrong.
If somebody have an idea where is the problem I will be so thankful.
> const int n=10;
int a[n][n];
int c[n];
int i,j,k,suma;
printf("vuvedete br redove i stalbove n=");
scanf ("%d",&n);
for (i=0;i<n;i++){
for (j=0;j<n;j++){
do{
printf("a[%d][%d]=",i,j);
scanf("%d",&a[i][j]);
}
while(a[i][j]<-1000||a[i][j]>1000);
}
}
suma=0;
for (i=0;i<n;i++){
for (j=0;j<n;j++){
if (a[i][j]>0){
suma=suma+1;}
}
}
for(k=0;k<n;k++){
printf("c[%d]=%d",k,suma);
}
return 0;
}
Main changes:
Populate array c[] and discard variable suma. This was the primary issue with your code.
You declare n as a const but then allow user to enter its value. I've removed the const and used a #define to set the max dimensions for the arrays.
Removed while loop - what's this for?
General tidying up.
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#define MAX 10
int main() {
int a[MAX][MAX] = {0};
int c[MAX] = {0};
int n = MAX;
printf("vuvedete br redove i stalbove n=");
scanf ("%d",&n);
for (int i=0;i<n;i++)
for (int j=0;j<n;j++) {
printf("a[%d][%d]=",i,j);
scanf("%d",&a[i][j]);
}
for (int i=0;i<n;i++)
for (int j=0;j<n;j++)
c[i] += a[i][j] > 0 ? 1 : 0;
for(int k=0;k<n;k++)
printf("c[%d]=%d\n",k,c[k]);
return 0;
}

Finding specific sum of a integers in an array

Basically this problem asks me to get an input of target_sum and check the integers in the array. If a sum of a pair is equal to target_sum, I need to print it.
I wrote the code like this but it does not work. Where did I make the mistake?
void findPair (int arr[],int size,int target_sum)
{
int i,j;
for (i=0;i<size;i++){
for (j=0;j<size;j++){
if (arr[i] + arr[j] == target_sum)
{
printf("(%d,%d)",arr[i],arr[j]);
}
else
{
printf("No pair.");
}
}
}
}
int main(){
int arr[] = {8,7,2,5,3,1};
int target_sum;
printf("Enter target sum: ");
scanf("%d",target_sum);
findPair(arr,6,target_sum);
}
As #nsilent22 suggested, scanf("%d",target_sum); should be changed into scanf("%d",&target_sum);. Besides that, you could have the second for loop start at j=i+1, making your code overall look like this:
#include <stdio.h>
void findPair (int arr[],int size,int target_sum)
{
int i,j;
for (i=0;i<size;i++)
for (j=i+1;j<size;j++)
if (arr[i] + arr[j] == target_sum)
printf("\n(%d,%d)",arr[i],arr[j]);
else
printf("\nNo pair.");
}
int main(){
int arr[] = {8,7,2,5,3,1};
int target_sum;
printf("Enter target sum: ");
scanf("%d",&target_sum);
findPair(arr,6,target_sum);
}

I am trying to create an array and fill it with numbers from 1 to 10. Why it doesn't work?

I am trying to create an array and fill it with numbers from 1 to 10. Why it doesn't work? After filling it all the numbers should be printed.
#include <stdio.h>
int main() {
int i;
int number[10];
for(i=1; i<=10; i++)
{
printf("%d\n",number[i]);
}
printf("\n");
return 0;
}
In C this first index is 0. Therefore the code should use indexes 0 through to 9
I.e.
#include <stdio.h>
int main() {
int i;
int number[10];
for(i=0; i<10; i++)
{
number[i] = 1 + i;
printf("%d\n",number[i]);
}
printf("\n");
return 0;
}

Creating, storing and outputting a 2D array in C

I am writing a code that uses a 2D array to store the house number and the number of children in the house, but I am having little trouble with the for loops and I don't really understand what the problem is. The dimensions of the array are the number of houses being the number of columns (this is a user input), and only two rows as the number of kids is inputted into the second row by the user. the code is shown below:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int i=0;
int j=0;
int houses=0;
int kids=0;
printf("How many houses are there in the street?\n");
scanf("%d", &houses);
int KidsInStreet[houses][2];//Columns are the houses and the rows are
the number of kids
for(i=0;i<houses;i++)
{
for(j=0;j<2;j++)
{
printf("Enter the value for KidsInStreet[%d][%d]:\n", i, j);
scanf("%d", &KidsInStreet[i][j]);
}
}
for (i=0;i<houses;i++)
{
for(j=0;j<2;j++)
{
printf("House number:%d. Number of kid(s):%d\n", KidsInStreet[i]
[j]);
}
}
return 0;
}
If I understood the requirement well you don't need 2 for loops. So your code is the following one:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int i=0;
int j=0;
int houses=0;
int kids=0;
printf("How many houses are there in the street?\n");
scanf("%d", &houses);
int KidsInStreet[houses][2];//Columns are the houses and the rows are the number of kids
for(i = 1; i <= houses; i++)
{
KidsInStreet [i][1] = i;
printf("Enter the value for KidsInStreet[%d]:\n", i);
scanf("%d", &KidsInStreet[i][2]);
}
for (i = 1; i <= houses; i++)
{
printf("House number:%d. Number of kid(s):%d\n", KidsInStreet[i][1], KidsInStreet[i][2]);
}
}

Coding a basic multi-threaded program

I want to create 2 threads, one does the max and one gives the average of a list of numbers entered in the command line.
#include <stdio.h>
#include <pthread.h>
#include <stdlib.h>
#include <limits.h>
void * thread1(int length, int array[] )
{
int ii = 0;
int smallest_value = INT_MAX;
for (; ii < length; ++ii)
{
if (array[ii] < smallest_value)
{
smallest_value = array[ii];
}
}
printf("smallest is: %d\n", smallest_value);
}
void * thread2()
{
printf("\n");
}
int main()
{
int average;
int min;
int max;
int how_many;
int i;
int status;
pthread_t tid1,tid2;
printf("How many numbers?: ");
scanf("%d",&how_many);
int ar[how_many];
printf("Enter the list of numbers: ");
for (i=0;i<how_many;i++){
scanf("%d",&ar[i]);
}
//for(i=0;i<how_many;i++)
//printf("%d\n",ar[i]);
pthread_create(&tid1,NULL,thread1(how_many,ar),NULL);
pthread_create(&tid2,NULL,thread2,NULL);
pthread_join(tid1,NULL);
pthread_join(tid2,NULL);
return 0;
exit(0);
}
I just made the first thread, which is to print out the min. number, but I have the following errors when compiling:
How many numbers?: 3
Enter the list of numbers: 1
2
3
Smallest: 1
Segmentation fault
How should I go on and fix the seg. fault?
You can't pass arguments like you're trying to in pthread_create.
Create a structure like:
struct args_t
{
int length;
int * array;
};
then initialize a structure with your array and length.
args_t *a = (args_t*)malloc(sizeof(args_t));
//initialize the array directly from your inputs
Then do
pthread_create(&tid1,NULL,thread1,(void*)a);
Then just cast the argument back to an args_t.
Hope that helps.

Resources