#include <stdio.h>
int main()
{
//Initialize array
int n; // n is use to decide the size of array
int x[n];
int y[n];
printf("enter the size of array:");
scanf("%d", &n);
if (n > 0)
{
for (int i = 0; i < n; i++)
{
printf("enter elements : \n");
scanf("%d", &x[i]);
}
}
printf("Array in reverse order: \n");
//Loop through the array in reverse order
for (int i = n - 1, j = 0; i >= 0; i--, j++)
{
y[j] = x[i];
printf("%d ", y[j]);
}
return 0;
}
In the above program, I have created a array whose size can be decided by the user. The user can also put elements in it.
After that I want to reverse this array and store the data in another array. But I get this error again and again. I am using CodeBlocks with the GCC compiler.
When the x and y arrays are created, n is uninitialized. There's no knowing how large these arrays will be. You loops are almost certainly accessing the array out of bounds.
You want to read n, then create the arrays. Of course you also want to error check the result of scanf.
int n; // n is use to decide the size of array
printf("enter the size of array:");
scanf("%d", &n);
int x[n];
int y[n];
I am new in c programmaing and I need your help. I have a project in which I am given three arrays A, B, C with pointer value 1..10. I want to a create a C program using the retrieve and update functions, in order to implement the sum of the arrays A:= B + C.
My code so far,
int main(int argc, char** argv) {
int v1[3],v2[3],v3[3];
for(int i = 0 ; i < 3; i++) {
printf("Type a number for v1 :\t");
scanf("%d", &v1[i]);
printf("Type a number for v2 :\t");
scanf("%d", &v2[i]);
// Add here
v3[i] = v1[i] + v2[i];
}
printf("\nResult Arr :\n");
for(int i = 0 ; i < 3; i++)
printf("%d\n", v3[i]);
}
But I seem that I totally miss the retrieve and update functions. According to my notes the retrieve function is equal to retrieve(S, c, i). The variable c get provided the value of component S which corresponds to the value of pointer i. The update(S, c, i) provides the value of variable c to the component of the array S which corresponds to the value of pointer i.
Any ideas on how to pproach this???
you should try both retrieve and update methods first, I'm pretty sure that is your homework and not the code you sent... Anyways, my approach would be, if I understand your homework correctly, that the c in the retrieve function has to be a pointer to set the value of array[i] and the c in the update has to be a value. I'm showing how I would make the retrieve one, and how to call it.
#include <stdio.h>
int retrieve (int * array, int *c, int i){
if (array[i]==NULL){
printf("i is out of range");
return -1; // You could also return max integer for error purposes
}
*c = array[i];
return 0; // Returned with no errors
}
int main(int argc, char** argv) {
int v1[3],v2[3],v3[3];
for(int i = 0 ; i < 3; i++) {
printf("Type a number for v1 :\t");
scanf("%d", &v1[i]);
printf("Type a number for v2 :\t");
scanf("%d", &v2[i]);
// Add here
v3[i] = v1[i] + v2[i];
}
int c=1;
retrieve(v1,&c,0);
printf("%d",c);
printf("\nResult Arr :\n");
for(int i = 0 ; i < 3; i++){
printf("%d\n", v3[i]);
}
}
I've been trying to apply all advices found in this site but none seems to be working.
For the first part of the code I need to fill an array with random numbers (0 or 1) to simulate an epidemic spreading, but the array obtained is not the desired one at all... this is the code I wrote:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main(int argc, char **argv)
{
int N, BC, t, T, i, v[N];
float b, g, p, r;
/*Variable values initialization*/
printf("Enter infection probability:\n");
scanf("%f", &b);
printf("Enter the number of individuals:\n");
scanf("%d", &N);
printf("Enter the number of time steps:\n");
scanf("%d", &T);
printf("Periodic boundary contitions? (Y:1 / N:0)\n");
scanf("%d", &BC);
/*First set of individuals*/
srand(time(NULL));
for(i = 0; i < N; i++){
v[i] = (rand()/RAND_MAX);
}
/*Check if array properly initialized*/
printf("Initial array:\n" );
for(i = 0; i < N; i++){
printf("%d-", v[i]);
}
The outcome I expected for the array was something like: 1-0-1-1-0-0-0-..., but I always get the following one:
Initial array:
0-0-2-15-0-0-0-0-0-0-
What am I doing wrong?
Thanks a million!
You should declare v[N] after
printf("Enter the number of individuals:\n");
scanf("%d", &N);
otherwise its size will be random since N isn't initialized when the memory allocated for v[] based on N is set.
If you want just 0 or 1 you should use a modulo:
srand(time(NULL));
for(i = 0; i < N; i++){
v[i] = (rand() % 2);
}
all the even values generated by rand will become 0 and all the odd values will become 1
This program is supposed to asks the user to enter a positive integer (the integer could be of any number of digits in the range of the integer type) and replace each digit by the sum of that digit plus 6 modulus 10. The program then should swap the first digit with the last digit before it displays the output.
A sample input/output:
Enter the number of digits of the number: 5
Enter the number: 92828
Output: 48485
For some reason with my code, no matter what number I enter, everything just comes out as 6. (so if I enter 5 numbers, I get 666666). I'm new to pointers, so is there an issue with that, or do I just have some math wrong? The program runs without any compiler warnings.
#include <stdio.h>
#include <stdlib.h>
void replace(int *a, int *b, int n);
void swap(int *p, int *q);
int main()
{
int n = 0;
int i = 0;
int a[100], b[100];
//Prompt user to enter number of digits
printf("Enter the number of digits you'd like to replace: ");
scanf("%d", &n);
//Prompt user to enter the number to use
printf("Enter the number to use: ");
for(i = 0; i < n; i++);
scanf("%1d", &a[i]);
//replace function
replace(a, b, n);
for(i = 0; i < n; i++)
printf("%d", b[i]);
printf("\n\n");
return 0;
}
void replace(int *a, int *b, int n)
{
int i;
for (i = 0; i < n; i++)
{
*(b+i) = (*(a+i)+ 6) % 10;
}
printf("The output is: ");
//swap function
swap(b, (b+ (n-1)));
}
void swap(int *p, int *q)
{
int t;
t = *p;
*p = *q;
*q = t;
}
Your code is absolutely correct except a silly mistake in the following code snippet.
for(i = 0; i < n; i++);
scanf("%1d", &a[i]);
Why did you put a ; after the for statement? It means your for loop is just iterating once (instead of 5 if n = 5). As a result, only the first digit input is considered given by the user but that too be stored in a[5] (considering n = 5), values stored in a[0] to a[4] are all garbage value.
Just remove the semicolon and update your code as follows.
for(i = 0; i < n; i++)
scanf("%1d", &a[i]);
Now it works fine.
The culprit in your code is the semicolon after the for loop:
for(i = 0; i < n; i++)**;**
scanf("%1d", &a[i]);
So the scanf that you wrote is basically out of the for-loop and stores the first digit into a[n].
I am fairly new around here and this year is the first time I am learning c. I have run into a problem concerning 2D arrays and such. The question is: Write a program that finds the sum of two 2D matrixes.
I can do this fairly easily but there is a problem I'm running into. For example I'll give the first set of arrays a length of 3x3.
If my first 2D array and the second array has:
{1,2,3; 4,5,6; 7,8,9} (1st array)
{0,0,0; 0,0,0; 0,0,0} (2nd array)
I am also given a the number of rows and columns by user. (User inputs 3x2) then it should appear like
{1,2; 3,4; 5,6} (OUTPUT)
but I am getting
{1,2; 3,5; 6,8}
Another example
User inputs 2x4 OUTPUT should be {1,2,3,4; 5,6,7,8}
What am I doing wrong here?
#include <stdio.h>
#include <stdlib.h>
#define MAXROW 3
#define MAXCOL 3
int main() {
int ray1[MAXROW][MAXCOL], ray2[MAXROW][MAXCOL];
int r, c;
printf("Enter the number of ROWS: ");
scanf("%d", &r);
printf("Enter the number of COLUMNS: ");
scanf("%d", &c);
int sumRay[r][c];
printf("\n");
printf("Input integers for Array %d.\n", 1);
arrayIN(ray1);
printRay(ray1);
printf("Input integers for Array %d.\n", 2);
arrayIN(ray2);
printRay(ray2);
arraySUM(ray1, ray2, r, c, sumRay);
printSumRay(r, c, sumRay);
//printRay(sumRay);
}
void arrayIN(int ray[MAXROW][MAXCOL]) {
int r, c;
for (r = 0; r < MAXROW; r++) {
for (c = 0; c < MAXCOL; c++) {
printf("Enter Number for [ROW:%d COL:%d]: ", r, c);
scanf("%d", &ray[r][c]);
}
}
}
void arraySUM(int ray1[MAXROW][MAXCOL], int ray2[MAXROW][MAXCOL],
int r, int c, int sumRay[r][c]) {
int i, j;
int x, y;
i = j = 0;
int sum;
for (x = 0; x <= r; x++, i++) {
if (i < MAXROW) {
for (y = 0; y <= c; y++, j++) {
if (j < MAXCOL) {
sum = ray1[i][j] + ray2[i][j];
sumRay[x][y]= sum;
} else {
j = 0;
}
}
} else {
i = 0;
}
}
printf("\n");
}
void printSumRay(int r, int c, int sumRay[r][c]) {
int i, j;
for (i = 0; i < r; i++) {
printf("\n");
for (j = 0; j < c; j++) {
printf("%d\t", sumRay[i][j]);
}
}
}
void printRay(int ray[MAXROW][MAXCOL]) {
int i, j;
for (i = 0; i < 3; i++) {
printf("\n");
for (j = 0; j < 3; j++) {
printf("%d\t", ray[i][j]);
}
}
printf("\n");
}
First off, you need to put function prototypes before main(), or at least move the function definitions before main(). It is not at all clear to me why you use a VLA for sumRay[][], but arrays of constant dimensions for ray1[][] and ray2[][]. Unless you have a compelling reason for doing so, it will be better to use VLA's all around.
You should be using the size_t type for variables that hold array indices. The scanf() and printf() statements that handle the size_t variables must then be modified to use the %zu conversion specifier.
The arraySum() function iterates over two pairs of duplicate array indices for reasons which are unclear. The logic here is convoluted and overly complex. The spurious output that you reported can be traced to this function; it is difficult to read and understand, which should be a sign that it needs to be rewritten. And if your intention is to add the input arrays only partially, the name does not reflect this intention. I have simplified this function, tightening the logic and removing the duplications. See the update below for a version that does partial array addition.
The printSumRay() function seems superfluous, since the printRay() function can do the same work, so I removed it, rewriting printRay() to use VLA's and tightening the code. The original code was using the magic number 3 in the controlling expressions of the loops here, instead of taking advantage of MAXROW and MAXCOL. But, even when not using VLA's, it is better practice to pass the dimensions to any function that will work with an array.
Here is a modified version of the original code:
#include <stdio.h>
#include <stdlib.h>
void arrayIN(size_t r, size_t c, int ray[r][c]);
void arraySUM(size_t r, size_t c, int ray1[r][c], int ray2[r][c], int sumRay[r][c]);
void printRay(size_t r, size_t c, int ray[r][c]);
int main(void)
{
size_t r,c;
printf("Enter the number of ROWS: ");
scanf("%zu", &r);
printf("Enter the number of COLUMNS: ");
scanf("%zu", &c);
int ray1[r][c], ray2[r][c], sumRay[r][c];
printf("\n");
printf("Input integers for Array %d.\n", 1);
arrayIN(r, c, ray1);
putchar('\n');
printRay(r, c, ray1);
putchar('\n');
printf("Input integers for Array %d.\n", 2);
arrayIN(r, c, ray2);
putchar('\n');
printRay(r, c, ray2);
putchar('\n');
arraySUM(r, c, ray1, ray2, sumRay);
printRay(r, c, sumRay);
putchar('\n');
return 0;
}
void arrayIN(size_t r, size_t c, int ray[r][c])
{
for(size_t i = 0; i < r; i++) {
for(size_t j = 0; j < c; j++) {
printf("Enter Number for [ROW:%zu COL:%zu]: ", i, j);
scanf("%d", &ray[i][j]);
}
}
}
void arraySUM(size_t r, size_t c, int ray1[r][c],int ray2[r][c], int sumRay[r][c])
{
for(size_t i = 0; i < r; i++) {
for(size_t j = 0; j < c; j++) {
sumRay[i][j] = ray1[i][j] + ray2[i][j];
}
}
}
void printRay(size_t r, size_t c, int ray[r][c])
{
for(size_t i = 0; i < r; i++) {
for(size_t j = 0; j < c; j++) {
printf("%8d",ray[i][j]);
}
putchar('\n');
}
}
As a next step, you might add some error-checking to the input code, checking the return values from the calls to scanf(). And, as it is, it is awkward to enter the numbers for the arrays, being prompted for each element. You might experiment with ways to improve this.
Update
If your true goal is to combine only the initial rows and columns of your arrays, the above code works with only minor modification. You should still use VLAs, but instead of defining the global constants MAXROW and MAXCOL, define const size_t maxrow and const size_t maxcol within the body of main(). You should be passing these array dimensions to the functions anyway, not relying on global values.
A function has been added, partArraySUM(), with a name that more closely captures its purpose. The only difference between this function and arraySUM() is that the input arrays ray1[][] and ray2[][] have different dimensions than the array sumRay[][] which holds the results. There is no need to keep separate indices for this.
#include <stdio.h>
#include <stdlib.h>
void arrayIN(size_t r, size_t c, int ray[r][c]);
void arraySUM(size_t r, size_t c, int ray1[r][c], int ray2[r][c], int sumRay[r][c]);
void partArraySUM(size_t r_sz, size_t c_sz, int ray1[r_sz][c_sz], int ray2[r_sz][c_sz], size_t r, size_t c, int sumRay[r][c]);
void printRay(size_t r, size_t c, int ray[r][c]);
int main(void)
{
const size_t maxrow = 3;
const size_t maxcol = 3;
size_t r,c;
printf("Enter the number of ROWS: ");
scanf("%zu", &r);
printf("Enter the number of COLUMNS: ");
scanf("%zu", &c);
int ray1[maxrow][maxcol], ray2[maxrow][maxcol], sumRay[r][c];
printf("\n");
printf("Input integers for Array %d.\n", 1);
arrayIN(maxrow, maxcol, ray1);
putchar('\n');
printRay(maxrow, maxcol, ray1);
putchar('\n');
printf("Input integers for Array %d.\n", 2);
arrayIN(maxrow, maxcol, ray2);
putchar('\n');
printRay(maxrow, maxcol, ray2);
putchar('\n');
partArraySUM(maxrow, maxcol, ray1, ray2, r, c, sumRay);
printRay(r, c, sumRay);
putchar('\n');
return 0;
}
void arrayIN(size_t r, size_t c, int ray[r][c])
{
for(size_t i = 0; i < r; i++) {
for(size_t j = 0; j < c; j++) {
printf("Enter Number for [ROW:%zu COL:%zu]: ", i, j);
scanf("%d", &ray[i][j]);
}
}
}
void arraySUM(size_t r, size_t c, int ray1[r][c],int ray2[r][c], int sumRay[r][c])
{
for(size_t i = 0; i < r; i++) {
for(size_t j = 0; j < c; j++) {
sumRay[i][j] = ray1[i][j] + ray2[i][j];
}
}
}
void partArraySUM(size_t r_sz, size_t c_sz, int ray1[r_sz][c_sz], int ray2[r_sz][c_sz], size_t r, size_t c, int sumRay[r][c])
{
for(size_t i = 0; i < r; i++) {
for(size_t j = 0; j < c; j++) {
sumRay[i][j] = ray1[i][j] + ray2[i][j];
}
}
}
void printRay(size_t r, size_t c, int ray[r][c])
{
for(size_t i = 0; i < r; i++) {
for(size_t j = 0; j < c; j++) {
printf("%8d",ray[i][j]);
}
putchar('\n');
}
}
Sample interaction:
Enter the number of ROWS: 2
Enter the number of COLUMNS: 2
Input integers for Array 1.
Enter Number for [ROW:0 COL:0]: 1
Enter Number for [ROW:0 COL:1]: 2
Enter Number for [ROW:0 COL:2]: 3
Enter Number for [ROW:1 COL:0]: 4
Enter Number for [ROW:1 COL:1]: 5
Enter Number for [ROW:1 COL:2]: 6
Enter Number for [ROW:2 COL:0]: 7
Enter Number for [ROW:2 COL:1]: 8
Enter Number for [ROW:2 COL:2]: 9
1 2 3
4 5 6
7 8 9
Input integers for Array 2.
Enter Number for [ROW:0 COL:0]: 1
Enter Number for [ROW:0 COL:1]: 1
Enter Number for [ROW:0 COL:2]: 1
Enter Number for [ROW:1 COL:0]: 1
Enter Number for [ROW:1 COL:1]: 1
Enter Number for [ROW:1 COL:2]: 1
Enter Number for [ROW:2 COL:0]: 1
Enter Number for [ROW:2 COL:1]: 1
Enter Number for [ROW:2 COL:2]: 1
1 1 1
1 1 1
1 1 1
2 3
5 6
Just a minor change is needed. In arraySUM(), inside the else part you have re initialized
i=0 and j=0.
But after the re initialization they are incremented. So it will become 1 so while execution it will read ray[1], not ray[0].
Just re initialize them to -1.
i=-1 and j=-1