The question i have is similar to some questions answered, but the answer for this in particular wasn't in there. Here is my code
int main()
{ int i,j,k,sorted;
int A[4][4];
int C[16];
int positive = 0;
for(i=0;i<4;i++)
{
for(j = 0; j<4; j++)
{
printf("A[%d][%d]: ", i,j);
scanf("%d", &A[i][j]);
if(A[i][j]>0){
C[positive] = A[i][j];
printf("C = %d\n");
positive++;
}
}
}
for(j=0;j<4;j++)
{
printf("%d ", A[positive]);
}
printf("\n");
//printf("Your positive numbers are: ", positive);
printf("\n");
system("pause");
return 0;
}
I want to check if the input is negative and add it to the 1D array only then. The question is how do I do that?
General Tips
int main()
{
int i,j,k,sorted; // do not declare loop control variables outside of their respective loops
int A[4][4];
int C[16];
int positive = 0; // for readability's sake please refrain from using long ass variable names for simple control var's
for(i=0;i<4;i++) // idiomatic way is for(int i = 0; i < 4; i++)
{
for(j = 0; j<4; j++) // for(int j = 0; j<4; j++)
{
printf("A[%d][%d]: ", i,j);
scanf("%d", &A[i][j]);
if(A[i][j]>0)
{
C[positive] = A[i][j];
printf("C = %d\n"); // missing argument & redundant since you intend to print them afterwards anyway?
positive++;
}
}
}
for(j=0;j<4;j++) // this loop is supposed to print all positive numbers?
{
printf("%d ", A[positive]); // you are looping through A[0-3][0], which are not necessarily the positive numbers of A.
}
printf("\n");
//printf("Your positive numbers are: ", positive);
Now to your question at hand. What the code tries to do and what you ask from us are two different things. Right now you are adding positive values to your C array, but your question asks about negative values?
Cleaned up code that behaves the way i think you want it to.
int main()
{
int A[4][4];
int C[16];
int k = 0;
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 4; j++)
{
printf("A[%d][%d]: ", i, j);
scanf("%d", &A[i][j]);
if (A[i][j] > 0)
{
C[k] = A[i][j];
k++;
}
}
}
printf("positive numbers in matrix A are: ");
for (int i = 0; i < k; i++) // loop from 0 to actual number of positive integers; array size remains at 16 in this case.
{
printf("%d ", C[i]);
}
printf("\n");
//printf("Your positive numbers are: ", positive);
printf("\n");
system("pause");
return 0;
}
int main()
{ int i,j,k,sorted;
int A[4][4];
int C[16];
int positive = 0;
for(i=0;i<4;i++)
{
for(j = 0; j<4; j++)
{
printf("A[%d][%d]: ", i,j);
scanf("%d", &A[i][j]);
if(A[i][j]>0){
C[positive] = A[i][j];
printf("C = %d\n");
positive++;
}
}
}
for(j=0;j<positive;j++)
{
printf("%d ", C[j]);
}
printf("\n");
//printf("Your positive numbers are: ", positive);
printf("\n");
system("pause");
return 0;
}
where you wrong is in the looping for result, you print a 2D array but you just use 1D array, and in the loop is not until 4 but until the positive, and then in the loop, you print A, but the result is C no A
Related
I want to create a program that can read scores from user and display the scores output in the form of stars. But it seems that my code keep getting an infinity loop. Can anyone tell me what is wrong with my looping.
#include <stdio.h>
int main() {
int i, k, j;
int score[5];
for(i = 1; i < 6; i++) {
printf("\nplease enter the score for %d match : ", i);
scanf("%d", &score[i]);
}
printf("\nstatistics collection points for 5 matches\n\n");
for (k = 1; k < 6; k++) {
printf("%d. ", k);
for (j = 1; j <= score[k]; j++) {
printf("*");
}
printf("\n");
}
return 0;
}
Array indices are 0 based in C. So for an array with 5 elements the indices are from 0-4 inclusive. You are using indices 1-5 which results in buffer overflow. Below is the corrected program with the for loops fixed up to use the right indices:
#include <stdio.h>
#define NUM_SCORES 5
int main()
{
int i, k, j;
int score[NUM_SCORES];
for(i = 0; i < NUM_SCORES ; i++)
{
printf("\nplease enter the score for %d match : ", i);
scanf("%d", &score[i]);
}
printf("\nstatistics collection points for 5 matches\n\n");
for (k = 0; k < NUM_SCORES ; k++){
printf("%d. ", k);
for(j = 1; j <= score[k]; j++){
printf("*");
}
printf("\n");
}
return 0;
}
You should also add a check to ensure scanf is able to successfully read each input.
So, my functions in my code don't talk to each other and I cannot for the life of me figure it out. What do you put in the parenthesis of the function headings. the variable names go in them but how are they formatted? Do I need pointers? If so can someone explain them. Thanks!
int FindTranspose(????)
int main(????)
FindTranspose (????)
#include <stdio.h>
int FindTranspose(int before[3][3], int after[3][3], int i, int j){
int rows, columns;
printf("\nThe orginial matrix: \n");
for(i=0;i<3;i++){
printf("\n");
for(j=0;j<3;j++){
printf("%d ", before[i][j]);
}
}
for(rows = 0; rows < i; rows++){
for(columns = 0;columns < j; columns++){
after[columns][rows] = before[rows][columns];
}
}
printf("\n");
printf("\nThe Transpose of the matrix: \n");
printf("\n");
for(rows = 0; rows < j; rows++){
for(columns = 0; columns < i; columns++){
printf("%d ", after[rows][columns]);
}
printf("\n");
}
}
int main(){
int before[3][3], after[3][3], i, j;
for(i=0;i<3;i++){
for(j=0;j<3;j++){
printf("Enter number for array - [%d],[%d] : ",i,j);
scanf("%d", &before[i][j]);
}
}
FindTranspose (before[3][3], after[3][3], i, j);
return 0;
}
Picture of code
Picture of warnings
Dealing with multi-dimensional arrays as pointers is really tricky in C.
For example the multi-dimensional array before[3][3] defined in main:
The array name before is not "as usual" a pointer to before[0][0] although it's a pointer to before[0] so before actually has the type int (*) [3] which read as 'Pointer to a one-dimensional array of 3 elements'
Anyway, you should learn more about "pointer and multi-dimensional arrays".
For the moment here is the solution to your issue:
#include <stdio.h>
void FindTranspose(int (*before)[3], int (*after)[3], int i, int j){
int rows, columns;
printf("\nThe orginial matrix: \n");
for(i=0;i<3;i++){
printf("\n");
for(j=0;j<3;j++){
printf("%d ", before[i][j]);
}
}
for(rows = 0; rows < i; rows++){
for(columns = 0;columns < j; columns++){
after[columns][rows] = before[rows][columns];
}
}
printf("\n");
printf("\nThe Transpose of the matrix: \n");
printf("\n");
for(rows = 0; rows < j; rows++){
for(columns = 0; columns < i; columns++){
printf("%d ", after[rows][columns]);
}
printf("\n");
}
}
int main(){
int before[3][3], after[3][3], i, j;
for(i=0;i<3;i++){
for(j=0;j<3;j++){
printf("Enter number for array - [%d],[%d] : ",i,j);
scanf("%d", &before[i][j]);
}
}
FindTranspose (before, after, i, j);
return 0;
}
Of course, there is an easier but complex to understand solution which is dealing with the multi-dimensional array as a one-dimensional array.
Here is an example of that:
#include <stdio.h>
#define NUM_OF_ROWS 3
#define NUM_OF_COLS 3
void FindTranspose(int *before){
int rows, columns, i;
int after[NUM_OF_ROWS][NUM_OF_COLS];
int *p, (*q)[NUM_OF_COLS];
printf("\nThe orginial matrix: \n");
i = 0;
for(p = before; p < before + NUM_OF_COLS * NUM_OF_ROWS; p++){
printf("%d ", *p);
if(++i >= NUM_OF_COLS){
i = 0;
printf("\n");
}
}
columns = 0;
for(p = before; p < before + NUM_OF_COLS * NUM_OF_ROWS;){
for(q = &after[0]; q < &after[NUM_OF_ROWS]; q++){
(*q)[columns] = *p++;
}
columns++;
}
printf("\n");
printf("\nThe Transpose of the matrix: \n");
for(rows = 0; rows < NUM_OF_ROWS; rows++){
for(columns = 0; columns < NUM_OF_COLS; columns++){
printf("%d ", after[rows][columns]);
}
printf("\n");
}
}
int main(){
int before[NUM_OF_ROWS][NUM_OF_COLS], i, j;
for(i=0; i<NUM_OF_ROWS; i++){
for(j=0; j<NUM_OF_COLS; j++){
printf("Enter number for array - [%d][%d]: ", i, j);
scanf("%d", &before[i][j]);
}
}
FindTranspose ( &before[0][0] );
return 0;
}
Recommended reading: C Programming A modern approach 2nd edition for K.N. King
Im taking a C programming beginning course, and have a problem with a for loop.
I want to make two double arrays. One that takes input from user, and one that sums the inputs.
I then want to output the two arrays. One that shows the input, and one that shows the sums og the input in every cell.
The problem comes when I try to show the sum for every "cell" in the array.
The output just becomes the same as the input.
I can solve it with:
// print all the numbers in the second array
for (i = 0; i < n; i++) {
sum = sum + a[i];
printf("%lf ", sum);
but then the assignment wouldn't be solved. Hope you can educate me.
#include <stdio.h> #include <stdlib.h>
int main(void) {
int n; //numbers of cells
int i; //The numbers in the cells
printf("How many cells in the array would you like? \n");
scanf("%d", &n);
double a[n], sum=0; // declare an array and a loop variable.
double b[n], sumo=0;
printf("Enter the numbers:\n");
for (i = 0; i < n; i++) { // read each number from the user
scanf("%lf", &a[i]);
}
printf("The results, my lord:\n");
// print all the numbers in the first array
for (i = 0; i < n; i++) {
printf("%lf ", a[i]);
}
printf("\n"); //THIS IS WHERE THE PROBLEM STARTS
// print all the numbers in the second array
for (i = 0; i < n; i++) {
b[i] = b[i] + a[i];
printf("%lf ", b[i]);
}
return 0;
}
For starters the array b is not initialized
double b[n], sumo=0;
So this statement
b[i] = b[i] + a[i];
invokes undefined behavior.
It seems what you need is the following for loop
for (i = 0; i < n; i++) {
b[i] = a[i];
if ( i != 0 ) b[i] += a[i-1];
printf("%lf ", b[i]);
}
You can calculate the sum (aka the b array) when you read the user data:
for (i = 0; i < n; i++)
{
scanf("%lf", &a[i]);
if (i == 0)
{
b[i] = a[i];
}
else
{
b[i] = b[i-1] + a[i];
}
}
or do it like:
scanf("%lf", &a[0]);
b[0] = a[0];
for (i = 1; i < n; i++)
{
scanf("%lf", &a[i]);
b[i] = b[i-1] + a[i];
}
or do it like:
sum = 0;
for (i = 0; i < n; i++)
{
scanf("%lf", &a[i]);
sum += a[i];
b[i] = sum;
}
The last part is how I solved the problem. I found the issue that. I didn't update the "sum" for ever loop. It adds a new value in b[I] this way. :-)
double a[n], sum=0; // declare an array and a loop variable.
double b[n];
printf("Enter the numbers:\n");
for (i = 0; i < n; i++) { // read each number from the user
scanf("%lf", &a[i]);
}
printf("The results, my lord:\n");
// print all the numbers in the first array
for (i = 0; i < n; i++) {
printf("%.2lf ", a[i]);
}
printf("\n");
// print all the numbers in the second array
for (i = 0; i < n; i++) {
sum = sum + a[i];
b[i] = sum;
printf("%.2lf ", b[i]);
I need to add two 3x3 matrices together using while loops. I am able to read and print both matrices using while loops but cannot work out how to add the matrices with while loops.
#include <stdio.h>
#include <stdlib.h>
int main()
{
int i=0,j=0,k=0,l=0,m=0,n=0;
int a[3][3],b[3][3],c[3][3];
printf("Enter the first matrix \n \n");
while(i<3)
{
j=0;
while(j<3)
{
scanf("%d",&a[i][j]);
j++;
}
i++;
}
printf("\n");
printf("The first matrix is \n\n");
i=0;
while(i<3)
{
j=0;
while(j<3)
{
printf("%d ",a[i][j]);
j++;
}
printf("\n");
i++;
}
printf("\n");
//////////////////////////////////////////////////////////////////////////
printf("Enter the second matrix \n \n");
while(k<3)
{
l=0;
while(l<3)
{
scanf("%d",&b[k][l]);
l++;
}
k++;
}
printf("\n");
printf("The second matrix is \n\n");
k=0;
while(k<3)
{
l=0;
while(l<3)
{
printf("%d ",b[k][l]);
l++;
}
printf("\n");
k++;
}
printf("\n");
///////////////////////////////////////////////////////////////////////////////
printf("The sum of the matrix's is \n \n");
return 0;
}
int matrix_a [3][3] = {{1,2,3},{3,4,5},{4,5,6}};
int matrix_b [3][3] = {{5,6,7},{6,7,8},{7,8,9}};
int i = 0;
while (i < 3) {
j = 0;
while (j < 3) {
matrix_a[i][j] += matrix[i][j];
++ j;
}
++ i;
}
for loops are a much better choice:
for (int i = 0; i < 3; ++ i) {
for (int j = 0; j < 3; ++ j) {
matrix_a[i][j] += matrix_b[i][j];
}
}
Superoptimal pointer-arithmetic alternative:
int* ptr_a = matrix_a;
int* ptr_b = matrix_b;
int size = 3 * 3;
while (size --) {
* ptr_a++ += * ptr_b++;
}
Break your problem down into smaller sub problems to help solve it.
EXAMPLE:
How to loop through a 2-D array?
How to get values and assign them to a variable from looping through the array?
How to manipulate the values to are obtaining (In your case adding them to something else)
How to insert these values into the correct position in a new array.
You can add the below code to do the addition:
k=0;
while(k<3)
{
l=0;
while(l<3)
{
c[k][l] = a[k][l]+b[k][l];
l++;
}
k++;
}
And, to display your matrix, you can use for loop instead of while.
Example:
for(int p =0;p<3;p++){
for(int q=0;q<3;q++){
c[p][q] = a[p][q] + b[p][q] ;
}
}
for(int p =0;p<3;p++){
for(int q=0;q<3;q++){
printf("%d \t",c[p][q]) ;
}
printf("\n");
}
I want to delete duplicates values in array. For example: array[1,5,6,1,3,5,9] I want to have array[6,3,9].
I have written this, but I am having troubles:
#include<stdio.h>
main() {
int array[50], i, j, k=0, c=0, array2[50], n;
printf("Enter array dimension: "); scanf("%d", &n);
for (i = 0; i < n; i++) {
printf("array[%d]= ", i); scanf("%d", &array[i]);
}
for (i = 0; i < n; ) {
for (j = i + 1; j < n; j++) {
if (array[i] == array[j])
i++;
else {
array2[k++] = array[i];
c++;
}
}
}
for (k = 0; k < c; k++) {
printf("%d ", array2[k]);
}
system("pause");
}
You should start by describing your problem in pseudo code, and breaking it into smaller pieces.
Start from scratch by deleting all these redundant variables, and try to implement the following algorithm:
for each element in inputArray
if not elementIsDuplicate(element)
add to outputArray
That's a single for loop. The elementIsDuplicate is separate function.
Now try to implement the function elementIsDuplicate. This function also contains a single loop, takes input parameters (int* array, int n, int currentIdx) and returns 1 or 0 indicating whether the element at currentIdx occurs anywhere else in the array.
#include<stdio.h>
int main(){
int array[50], i, j, k=0, c, n, array2[50] = {0};
printf("Enter array dimension: "); scanf("%d", &n);
for (i = 0; i < n; ++i){
int num, dup = 0;
printf("array[%d]= ", i); scanf("%d", &num);
for(j = 0; j < k; ++j){
if(array[j] == num){
array2[j] = dup = 1;
break;
}
}
if(!dup){
array[k++] = num;
}
}
for (c=i=0; i < k; ++i){
if(!array2[i])
printf("%d ", array[c++] = array[i]);
}
printf("\n");
/*
for(i=0;i<c;++i)
printf("%d ", array[i]);
printf("\n");
*/
system("pause");
return 0;
}
#include<stdio.h>
main()
{
int n, a[50], b[50], count = 0, c, d;
printf("Enter number of elements in array\n");
scanf("%d",&n);
printf("Enter %d integers\n", n);
for(c=0;c<n;c++)
scanf("%d",&a[c]); //enter array elements
for(c=0;c<n;c++)
{
for(d=0;d<count;d++)
{
if(a[c]==b[d])
break;
}
if(d==count)
{
b[count] = a[c];
count++;
}
}
printf("count is: %d\n",count);
printf("Array obtained after removing duplicate elements\n");
for(c=0;c<count;c++)
printf("%d\n",b[c]);
return 0;
}
This will remove multiple duplicates from the desired array..
Example: if the input array is: 3 6 5 6 2 8 6 5 9 8 6 ,,then the output array will be: 3 6 5 2 8 9